package com.cq.hd.app.biz;

import com.cq.hd.app.dto.*;
import com.cq.hd.app.vo.*;
import com.cq.hd.common.constant.Constant;
import com.cq.hd.common.enums.ResponseEnums;
import com.cq.hd.common.enums.biz.OrderSettleBillTradeTypeEnum;
import com.cq.hd.common.enums.biz.OrderStateEnum;
import com.cq.hd.common.enums.biz.SettleStateEnum;
import com.cq.hd.common.enums.biz.SubOrderStateEnum;
import com.cq.hd.common.response.Throw;
import com.cq.hd.common.utils.LocalDateUtil;
import com.cq.hd.common.utils.StringUtils;
import com.cq.hd.member.api.TbBusinessAgentApi;
import com.cq.hd.member.api.TbBusinessApi;
import com.cq.hd.member.api.TbBusinessSubscribeApi;
import com.cq.hd.member.api.dto.BusinessAgentDto;
import com.cq.hd.member.api.dto.BusinessSubscribeDto;
import com.cq.hd.member.api.vo.BusinessAgentVo;
import com.cq.hd.member.api.vo.BusinessPageVo;
import com.cq.hd.member.api.vo.BusinessSubscribeVo;
import com.cq.hd.order.api.TbOrderApi;
import com.cq.hd.order.api.TbOrderFirstRecordApi;
import com.cq.hd.order.api.TbOrderItemApi;
import com.cq.hd.order.api.TbOrderSettleBillApi;
import com.cq.hd.order.api.dto.OrderDto;
import com.cq.hd.order.api.dto.OrderFirstRecordDto;
import com.cq.hd.order.api.dto.OrderItemDto;
import com.cq.hd.order.api.dto.OrderSettleBillDto;
import com.cq.hd.order.api.vo.OrderFirstRecordVo;
import com.cq.hd.order.api.vo.OrderItemVo;
import com.cq.hd.order.api.vo.OrderSettleBillVo;
import com.cq.hd.order.api.vo.OrderVo;
import com.cq.hd.product.api.TbActiveBrowseApi;
import com.cq.hd.product.api.TbActiveBrowseStatisticsApi;
import com.cq.hd.product.api.TbActiveShareApi;
import com.cq.hd.product.api.dto.ActiveBrowseDto;
import com.cq.hd.product.api.dto.ActiveBrowseStatisticsDto;
import com.cq.hd.product.api.dto.ActiveShareStatisticsDto;
import com.cq.hd.product.api.vo.ActiveBrowseStatisticsVo;
import com.cq.hd.product.api.vo.ActiveBrowseVo;
import com.cq.hd.product.api.vo.ActiveShareStatisticsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collectors;

@Component
@Slf4j
public class StatisticsBiz {

    @Autowired
    private TbBusinessApi businessApi;

    @Autowired
    private TbOrderApi orderApi;

    @Autowired
    private TbOrderItemApi orderItemApi;

    @Autowired
    private TbActiveBrowseApi activeBrowseApi;

    @Autowired
    private TbActiveBrowseStatisticsApi activeBrowseStatisticsApi;

    @Autowired
    private TbOrderFirstRecordApi orderFirstRecordApi;

    @Autowired
    private TbActiveShareApi activeShareApi;

    @Autowired
    private TbBusinessSubscribeApi businessSubscribeApi;

    @Autowired
    private TbBusinessAgentApi businessAgentApi;

    @Autowired
    private TbOrderSettleBillApi orderSettleBillApi;

    public AllDataStatisticsVo getAllData(AllDataStatisticsDto allDataStatisticsDto) {
        Long businessId = allDataStatisticsDto.getBusinessId();
        if (businessId == null || businessId <= 0) {
            Long userId = allDataStatisticsDto.getUserId();
            if (userId == null || userId <= 0) {
                Throw.isBusinessException(ResponseEnums.USER_ACCESS_DENIED);
            }

            BusinessPageVo businessPageVo = businessApi.getByUserId(userId).unpack();
            if (businessPageVo == null) {
                Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
            }

            allDataStatisticsDto.setBusinessId(businessPageVo.getId());
        }

        // 日期类型：1-今日，2-本周，3-本月，4-自定义
        Integer dateType = allDataStatisticsDto.getDateType();
        if (dateType == null) {
            Throw.isBusinessException("请选择查询日期类型");
        }

        LocalDate nowLocalDate = LocalDate.now();
        if (dateType == 1) {
            String nowDateStr = LocalDateUtil.toLocalDateToString(nowLocalDate, Constant.YYYY_MM_DD);

            allDataStatisticsDto.setStartTime(nowDateStr + " 00:00:00");
            allDataStatisticsDto.setEndTime(nowDateStr + " 23:59:59");
        } else if (dateType == 2) {
            // 本周开始日期
            LocalDate monday = nowLocalDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            // 本周结束日期
            LocalDate sunday = nowLocalDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));

            String mondayStartStr = LocalDateUtil.toLocalDateToString(monday, Constant.YYYY_MM_DD);
            String sundayEndStr = LocalDateUtil.toLocalDateToString(sunday, Constant.YYYY_MM_DD);

            allDataStatisticsDto.setStartTime(mondayStartStr + " 00:00:00");
            allDataStatisticsDto.setEndTime(sundayEndStr + " 23:59:59");
        } else if (dateType == 3) {
            // 本月1号
            LocalDate firstDayOfMonth = nowLocalDate.with(TemporalAdjusters.firstDayOfMonth());
            // 本月最后一天
            LocalDate lastDayOfMonth = nowLocalDate.with(TemporalAdjusters.lastDayOfMonth());

            String firstDayOfMonthStartStr = LocalDateUtil.toLocalDateToString(firstDayOfMonth, Constant.YYYY_MM_DD);
            String lastDayOfMonthEndStr = LocalDateUtil.toLocalDateToString(lastDayOfMonth, Constant.YYYY_MM_DD);

            allDataStatisticsDto.setStartTime(firstDayOfMonthStartStr + " 00:00:00");
            allDataStatisticsDto.setEndTime(lastDayOfMonthEndStr + " 23:59:59");
        } else if (dateType == 4) {
            String startTime = allDataStatisticsDto.getStartTime();
            String endTime = allDataStatisticsDto.getEndTime();
            if (StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)) {
                Throw.isBusinessException("请选择查询日期");
            }

            LocalDate startLocalDate = null;
            try {
                startLocalDate = LocalDateUtil.toLocalDate(startTime, Constant.YYYY_MM_DD);
            } catch (Exception e) {
                Throw.isBusinessException("查询开始日期格式错误");
            }

            LocalDate endLocalDate = null;
            try {
                endLocalDate = LocalDateUtil.toLocalDate(endTime, Constant.YYYY_MM_DD);
            } catch (Exception e) {
                Throw.isBusinessException("查询结束日期格式错误");
            }

            // 自定义日期最长不超过3个月
            long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate);
            if (days > 90) {
                Throw.isBusinessException("自定义日期最长不超过90天");
            }

            allDataStatisticsDto.setStartTime(startTime + " 00:00:00");
            allDataStatisticsDto.setEndTime(endTime + " 23:59:59");
        }

        // 总订单金额(实付金额)
        BigDecimal totalOrderAmt = BigDecimal.ZERO;
        // 总订单数
        int totalOrderNum = 0;
        // 总下单人数
        int totalOrderUserNum = 0;
        // 已结算补贴金额
        BigDecimal totalSubsidyAmt = BigDecimal.ZERO;
        // 退款金额
        BigDecimal refundOrderAmt = BigDecimal.ZERO;
        // 浏览人数
        int browseUserNum = 0;
        // 浏览次数
        int browseNum = 0;
        // 下单新用户
        int newOrderUserNum = 0;

        // 根据日期时间查询商家所有的订单数据,获取总订单金额（实付金额）,总订单数,总下单人数,退款金额
        OrderDto orderDto = new OrderDto();
        orderDto.setBusinessId(businessId);
        orderDto.setStartOrderTime(allDataStatisticsDto.getStartTime());
        orderDto.setEndOrderTime(allDataStatisticsDto.getEndTime());
        orderDto.setOrderStates(Arrays.asList(OrderStateEnum.WAIT_VERIFY.getValue(), OrderStateEnum.VERIFYING.getValue(),
                OrderStateEnum.FINISH.getValue(), OrderStateEnum.VERIFIED.getValue(), OrderStateEnum.CANCEL.getValue()));
        List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
        if (!CollectionUtils.isEmpty(orderVos)) {
            List<OrderVo> successOrderList = new ArrayList<>();
            for (OrderVo orderVo : orderVos) {
                Integer orderState = orderVo.getOrderState();
                if (OrderStateEnum.WAIT_VERIFY.getValue().equals(orderState) || OrderStateEnum.VERIFYING.getValue().equals(orderState)
                        || OrderStateEnum.FINISH.getValue().equals(orderState) || OrderStateEnum.VERIFIED.getValue().equals(orderState)) {
                    // 实付金额
                    BigDecimal payPrice = orderVo.getPayPrice();
                    // 总订单金额
                    totalOrderAmt = totalOrderAmt.add(payPrice);
                    // 总订单数
                    totalOrderNum += 1;

                    successOrderList.add(orderVo);
                } else if (OrderStateEnum.CANCEL.getValue().equals(orderState)) {
                    // 实付金额
                    BigDecimal payPrice = orderVo.getPayPrice();
                    // 总退款金额
                    refundOrderAmt = refundOrderAmt.add(payPrice);
                }
            }

            // 总下单人数
            if (!CollectionUtils.isEmpty(successOrderList)) {
                totalOrderUserNum = successOrderList.stream().map(OrderVo::getUserId).collect(Collectors.toSet()).size();
            }
        }

        // 根据交易时间+商家id查询已结算补贴金额（已结算的商品补贴+佣金补贴）
        OrderSettleBillDto orderSettleBillDto = new OrderSettleBillDto();
        orderSettleBillDto.setBusinessId(businessId);
        orderSettleBillDto.setStartTradeTime(allDataStatisticsDto.getStartTime());
        orderSettleBillDto.setEndTradeTime(allDataStatisticsDto.getEndTime());
        orderSettleBillDto.setTradeTypes(Arrays.asList(OrderSettleBillTradeTypeEnum.SUBSIDY_AMT.getValue(),
                OrderSettleBillTradeTypeEnum.COMMISSION_AMT.getValue()));
        List<OrderSettleBillVo> orderSettleBillVos = orderSettleBillApi.listByDto(orderSettleBillDto).unpack();
        if (!CollectionUtils.isEmpty(orderSettleBillVos)) {
            for (OrderSettleBillVo orderSettleBillVo : orderSettleBillVos) {
                BigDecimal tradeAmt = orderSettleBillVo.getTradeAmt();
                totalSubsidyAmt = totalSubsidyAmt.add(tradeAmt);
            }
        }

        // 根据日期查询浏览数据，获取浏览人数，浏览次数
        ActiveBrowseDto activeBrowseDto = new ActiveBrowseDto();
        activeBrowseDto.setBusinessId(businessId);
        activeBrowseDto.setStartBrowseTime(allDataStatisticsDto.getStartTime());
        activeBrowseDto.setEndBrowseTime(allDataStatisticsDto.getEndTime());
        List<ActiveBrowseVo> activeBrowseVos = activeBrowseApi.listByDto(activeBrowseDto).unpack();
        if (!CollectionUtils.isEmpty(activeBrowseVos)) {
            browseUserNum = activeBrowseVos.stream().map(ActiveBrowseVo::getUserId).collect(Collectors.toSet()).size();
        }

        ActiveBrowseStatisticsDto activeBrowseStatisticsDto = new ActiveBrowseStatisticsDto();
        activeBrowseStatisticsDto.setBusinessId(businessId);
        activeBrowseStatisticsDto.setStartCreateTime(allDataStatisticsDto.getStartTime());
        activeBrowseStatisticsDto.setEndCreateTime(allDataStatisticsDto.getEndTime());
        List<ActiveBrowseStatisticsVo> activeBrowseStatisticsVos = activeBrowseStatisticsApi.listByDto(activeBrowseStatisticsDto).unpack();
        if (!CollectionUtils.isEmpty(activeBrowseStatisticsVos)) {
            for (ActiveBrowseStatisticsVo activeBrowseStatisticsVo : activeBrowseStatisticsVos) {
                browseNum = browseNum + Integer.parseInt(String.valueOf(activeBrowseStatisticsVo.getBrowseNum()));
            }
        }

        // 根据日期查询下单新用户
        OrderFirstRecordDto orderFirstRecordDto = new OrderFirstRecordDto();
        orderFirstRecordDto.setBusinessId(businessId);
        orderFirstRecordDto.setStartOrderTime(allDataStatisticsDto.getStartTime());
        orderFirstRecordDto.setEndOrderTime(allDataStatisticsDto.getEndTime());
        List<OrderFirstRecordVo> orderFirstRecordVos = orderFirstRecordApi.listByDto(orderFirstRecordDto).unpack();
        if (!CollectionUtils.isEmpty(orderFirstRecordVos)) {
            newOrderUserNum = orderFirstRecordVos.stream().map(OrderFirstRecordVo::getUserId).collect(Collectors.toSet()).size();
        }

        AllDataStatisticsVo allDataStatisticsVo = new AllDataStatisticsVo();
        allDataStatisticsVo.setTotalOrderAmt(totalOrderAmt);
        allDataStatisticsVo.setTotalOrderNum(totalOrderNum);
        allDataStatisticsVo.setTotalOrderUserNum(totalOrderUserNum);
        allDataStatisticsVo.setTotalSubsidyAmt(totalSubsidyAmt);
        allDataStatisticsVo.setBrowseUserNum(browseUserNum);
        allDataStatisticsVo.setBrowseNum(browseNum);
        allDataStatisticsVo.setNewOrderUserNum(newOrderUserNum);
        allDataStatisticsVo.setRefundOrderAmt(refundOrderAmt);

        return allDataStatisticsVo;
    }

    public List<AllDataTrendStatisticsVo> getAllDataTrend(AllDataTrendStatisticsDto allDataTrendStatisticsDto) {
        Long businessId = allDataTrendStatisticsDto.getBusinessId();
        if (businessId == null || businessId <= 0) {
            Long userId = allDataTrendStatisticsDto.getUserId();
            if (userId == null || userId <= 0) {
                Throw.isBusinessException(ResponseEnums.USER_ACCESS_DENIED);
            }

            BusinessPageVo businessPageVo = businessApi.getByUserId(userId).unpack();
            if (businessPageVo == null) {
                Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
            }

            allDataTrendStatisticsDto.setBusinessId(businessPageVo.getId());
        }

        // 类型：1-总订单金额，2-总订单数，3-总下单人数，4-已结算补贴金额，5-浏览人数，6-浏览次数，7-下单新用户，8-退款金额
        Integer type = allDataTrendStatisticsDto.getType();
        if (type == null) {
            Throw.isBusinessException("请选择查询类型");
        }

        // 日期类型：1-今日，2-本周，3-本月，4-自定义
        Integer dateType = allDataTrendStatisticsDto.getDateType();
        if (dateType == null) {
            Throw.isBusinessException("请选择查询日期类型");
        }

        LocalDate nowLocalDate = LocalDate.now();
        if (dateType == 1) {
            String nowDateStr = LocalDateUtil.toLocalDateToString(nowLocalDate, Constant.YYYY_MM_DD);

            allDataTrendStatisticsDto.setStartTime(nowDateStr + " 00:00:00");
            allDataTrendStatisticsDto.setEndTime(nowDateStr + " 23:59:59");
        } else if (dateType == 2) {
            // 本周开始日期
            LocalDate monday = nowLocalDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            // 本周结束日期
            LocalDate sunday = nowLocalDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));

            String mondayStartStr = LocalDateUtil.toLocalDateToString(monday, Constant.YYYY_MM_DD);
            String sundayEndStr = LocalDateUtil.toLocalDateToString(sunday, Constant.YYYY_MM_DD);

            allDataTrendStatisticsDto.setStartTime(mondayStartStr + " 00:00:00");
            allDataTrendStatisticsDto.setEndTime(sundayEndStr + " 23:59:59");
        } else if (dateType == 3) {
            // 本月1号
            LocalDate firstDayOfMonth = nowLocalDate.with(TemporalAdjusters.firstDayOfMonth());
            // 本月最后一天
            LocalDate lastDayOfMonth = nowLocalDate.with(TemporalAdjusters.lastDayOfMonth());

            String firstDayOfMonthStartStr = LocalDateUtil.toLocalDateToString(firstDayOfMonth, Constant.YYYY_MM_DD);
            String lastDayOfMonthEndStr = LocalDateUtil.toLocalDateToString(lastDayOfMonth, Constant.YYYY_MM_DD);

            allDataTrendStatisticsDto.setStartTime(firstDayOfMonthStartStr + " 00:00:00");
            allDataTrendStatisticsDto.setEndTime(lastDayOfMonthEndStr + " 23:59:59");
        } else if (dateType == 4) {
            String startTime = allDataTrendStatisticsDto.getStartTime();
            String endTime = allDataTrendStatisticsDto.getEndTime();
            if (StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)) {
                Throw.isBusinessException("请选择查询日期");
            }

            LocalDate startLocalDate = null;
            try {
                startLocalDate = LocalDateUtil.toLocalDate(startTime, Constant.YYYY_MM_DD);
            } catch (Exception e) {
                Throw.isBusinessException("查询开始日期格式错误");
            }

            LocalDate endLocalDate = null;
            try {
                endLocalDate = LocalDateUtil.toLocalDate(endTime, Constant.YYYY_MM_DD);
            } catch (Exception e) {
                Throw.isBusinessException("查询结束日期格式错误");
            }

            // 自定义日期最长不超过3个月
            long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate);
            if (days > 90) {
                Throw.isBusinessException("自定义日期最长不超过90天");
            }

            allDataTrendStatisticsDto.setStartTime(startTime + " 00:00:00");
            allDataTrendStatisticsDto.setEndTime(endTime + " 23:59:59");
        }

        // 类型：1-总订单金额，2-总订单数，3-总下单人数，4-已结算补贴金额，5-浏览人数，6-浏览次数，7-下单新用户，8-退款金额
        if (type == 1) {
            // 总订单金额
            Map<String, BigDecimal> map = new HashMap<>();

            // 根据日期时间查询订单数据，获取总订单金额（实付金额）
            OrderDto orderDto = new OrderDto();
            orderDto.setBusinessId(businessId);
            orderDto.setStartOrderTime(allDataTrendStatisticsDto.getStartTime());
            orderDto.setEndOrderTime(allDataTrendStatisticsDto.getEndTime());
            orderDto.setOrderStates(Arrays.asList(OrderStateEnum.WAIT_VERIFY.getValue(), OrderStateEnum.VERIFYING.getValue(),
                    OrderStateEnum.FINISH.getValue(), OrderStateEnum.VERIFIED.getValue()));
            List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
            if (!CollectionUtils.isEmpty(orderVos)) {
                for (OrderVo orderVo : orderVos) {
                    // 实付金额
                    BigDecimal payPrice = orderVo.getPayPrice();
                    // 下单时间
                    LocalDateTime orderTime = orderVo.getOrderTime();

                    String mapKey = getMapKey(dateType, orderTime);

                    BigDecimal amt = map.get(mapKey);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }
                    amt = amt.add(payPrice);
                    map.put(mapKey, amt);
                }
            }

            List<AllDataTrendStatisticsVo> allDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    BigDecimal bigDecimal = map.get(date);
                    if (bigDecimal == null) {
                        bigDecimal = BigDecimal.ZERO;
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(bigDecimal.toString());
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(allDataTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(allDataTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    BigDecimal bigDecimal = map.get(date);
                    if (bigDecimal == null) {
                        bigDecimal = BigDecimal.ZERO;
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(bigDecimal.toString());
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return allDataTrendStatisticsVos;
        } else if (type == 2) {
            // 总订单数
            Map<String, Integer> map = new HashMap<>();

            OrderDto orderDto = new OrderDto();
            orderDto.setBusinessId(businessId);
            orderDto.setStartOrderTime(allDataTrendStatisticsDto.getStartTime());
            orderDto.setEndOrderTime(allDataTrendStatisticsDto.getEndTime());
            orderDto.setOrderStates(Arrays.asList(OrderStateEnum.WAIT_VERIFY.getValue(), OrderStateEnum.VERIFYING.getValue(),
                    OrderStateEnum.FINISH.getValue(), OrderStateEnum.VERIFIED.getValue()));
            List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
            if (!CollectionUtils.isEmpty(orderVos)) {
                for (OrderVo orderVo : orderVos) {
                    // 下单时间
                    LocalDateTime orderTime = orderVo.getOrderTime();
                    String mapKey = getMapKey(dateType, orderTime);
                    Integer count = map.get(mapKey);
                    if (count == null) {
                        count = 0;
                    }
                    count += 1;
                    map.put(mapKey, count);
                }
            }


//            OrderItemDto orderItemDto = new OrderItemDto();
//            orderItemDto.setBusinessId(businessId);
//            orderItemDto.setStartOrderTime(allDataTrendStatisticsDto.getStartTime());
//            orderItemDto.setEndOrderTime(allDataTrendStatisticsDto.getEndTime());
//            List<OrderItemVo> orderItemVos = orderItemApi.listByDto(orderItemDto).unpack();
//            if (!CollectionUtils.isEmpty(orderItemVos)) {
//                // 成功下单的订单项列表
//                List<OrderItemVo> orderItemVoList = orderItemVos.stream().filter(orderItemVo ->
//                        (SubOrderStateEnum.WAIT_VERIFY.getValue().equals(orderItemVo.getSubOrderState())
//                                || SubOrderStateEnum.VERIFIED.getValue().equals(orderItemVo.getSubOrderState()))).collect(Collectors.toList());
//                if (!CollectionUtils.isEmpty(orderItemVoList)) {
//                    for (OrderItemVo orderItemVo : orderItemVoList) {
//                        // 下单时间
//                        LocalDateTime orderTime = orderItemVo.getOrderTime();
//                        String mapKey = getMapKey(dateType, orderTime);
////                        String mapKey = LocalDateUtil.toLocalDateToString(orderTime.toLocalDate(), key);
//                        List<OrderItemVo> orderItems = map.get(mapKey);
//                        if (CollectionUtils.isEmpty(orderItems)) {
//                            orderItems = new ArrayList<>();
//                        }
//                        orderItems.add(orderItemVo);
//                        map.put(mapKey, orderItems);
//                    }
//                }
//            }

            List<AllDataTrendStatisticsVo> allDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    int orderNum = 0;
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    Integer count = map.get(date);
                    if (count != null) {
                        orderNum = count;
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(orderNum));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(allDataTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(allDataTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    int orderNum = 0;
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    Integer count = map.get(date);
                    if (count != null) {
                        orderNum = count;
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(orderNum));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return allDataTrendStatisticsVos;
        } else if (type == 3) {
            // 总下单人数
            Map<String, Integer> map = new HashMap<>();


            OrderDto orderDto = new OrderDto();
            orderDto.setBusinessId(businessId);
            orderDto.setStartOrderTime(allDataTrendStatisticsDto.getStartTime());
            orderDto.setEndOrderTime(allDataTrendStatisticsDto.getEndTime());
            orderDto.setOrderStates(Arrays.asList(OrderStateEnum.WAIT_VERIFY.getValue(), OrderStateEnum.VERIFYING.getValue(),
                    OrderStateEnum.FINISH.getValue(), OrderStateEnum.VERIFIED.getValue()));
            List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
            if (!CollectionUtils.isEmpty(orderVos)) {
                for (OrderVo orderVo : orderVos) {
                    // 下单时间
                    LocalDateTime orderTime = orderVo.getOrderTime();
                    String mapKey = getMapKey(dateType, orderTime);
//                        String mapKey = LocalDateUtil.toLocalDateToString(orderTime.toLocalDate(), key);
                    Integer count = map.get(mapKey);
                    if (count == null) {
                        count = 0;
                    }
                    count += 1;
                    map.put(mapKey, count);
                }
            }


//            OrderItemDto orderItemDto = new OrderItemDto();
//            orderItemDto.setBusinessId(businessId);
//            orderItemDto.setStartOrderTime(allDataTrendStatisticsDto.getStartTime());
//            orderItemDto.setEndOrderTime(allDataTrendStatisticsDto.getEndTime());
//            List<OrderItemVo> orderItemVos = orderItemApi.listByDto(orderItemDto).unpack();
//            if (!CollectionUtils.isEmpty(orderItemVos)) {
//                // 成功下单的订单项列表
//                List<OrderItemVo> orderItemVoList = orderItemVos.stream().filter(orderItemVo ->
//                        (SubOrderStateEnum.WAIT_VERIFY.getValue().equals(orderItemVo.getSubOrderState())
//                                || SubOrderStateEnum.VERIFIED.getValue().equals(orderItemVo.getSubOrderState()))).collect(Collectors.toList());
//                if (!CollectionUtils.isEmpty(orderItemVoList)) {
//                    for (OrderItemVo orderItemVo : orderItemVoList) {
//                        // 下单时间
//                        LocalDateTime orderTime = orderItemVo.getOrderTime();
//                        String mapKey = getMapKey(dateType, orderTime);
////                        String mapKey = LocalDateUtil.toLocalDateToString(orderTime.toLocalDate(), key);
//                        List<OrderItemVo> orderItems = map.get(mapKey);
//                        if (CollectionUtils.isEmpty(orderItems)) {
//                            orderItems = new ArrayList<>();
//                        }
//                        orderItems.add(orderItemVo);
//                        map.put(mapKey, orderItems);
//                    }
//                }
//            }

            List<AllDataTrendStatisticsVo> allDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    int orderUserNum = 0;
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    Integer count = map.get(date);
                    if (count != null) {
                        orderUserNum = count;
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(orderUserNum));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(allDataTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(allDataTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    int orderUserNum = 0;
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    Integer count = map.get(date);
                    if (count != null) {
                        orderUserNum = count;
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(orderUserNum));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return allDataTrendStatisticsVos;
        } else if (type == 4) {
            // 已结算补贴金额
            Map<String, BigDecimal> map = new HashMap<>();

            OrderSettleBillDto orderSettleBillDto = new OrderSettleBillDto();
            orderSettleBillDto.setBusinessId(businessId);
            orderSettleBillDto.setStartTradeTime(allDataTrendStatisticsDto.getStartTime());
            orderSettleBillDto.setEndTradeTime(allDataTrendStatisticsDto.getEndTime());
            orderSettleBillDto.setTradeTypes(Arrays.asList(OrderSettleBillTradeTypeEnum.SUBSIDY_AMT.getValue(),
                    OrderSettleBillTradeTypeEnum.COMMISSION_AMT.getValue()));
            List<OrderSettleBillVo> orderSettleBillVos = orderSettleBillApi.listByDto(orderSettleBillDto).unpack();
            if (!CollectionUtils.isEmpty(orderSettleBillVos)) {
                for (OrderSettleBillVo orderSettleBillVo : orderSettleBillVos) {
                    LocalDateTime tradeTime = orderSettleBillVo.getTradeTime();
                    BigDecimal tradeAmt = orderSettleBillVo.getTradeAmt();

                    String mapKey = getMapKey(dateType, tradeTime);
                    BigDecimal amt = map.get(mapKey);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }
                    amt = amt.add(tradeAmt);
                    map.put(mapKey, amt);
                }
            }

            List<AllDataTrendStatisticsVo> allDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    BigDecimal bigDecimal = map.get(date);
                    if (bigDecimal == null) {
                        bigDecimal = BigDecimal.ZERO;
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(bigDecimal.toString());
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(allDataTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(allDataTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    BigDecimal bigDecimal = map.get(date);
                    if (bigDecimal == null) {
                        bigDecimal = BigDecimal.ZERO;
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(bigDecimal.toString());
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return allDataTrendStatisticsVos;
        } else if (type == 5) {
            // 浏览人数
            Map<String, List<ActiveBrowseVo>> map = new HashMap<>();

            ActiveBrowseDto activeBrowseDto = new ActiveBrowseDto();
            activeBrowseDto.setBusinessId(businessId);
            activeBrowseDto.setStartBrowseTime(allDataTrendStatisticsDto.getStartTime());
            activeBrowseDto.setEndBrowseTime(allDataTrendStatisticsDto.getEndTime());
            List<ActiveBrowseVo> activeBrowseVos = activeBrowseApi.listByDto(activeBrowseDto).unpack();
            if (!CollectionUtils.isEmpty(activeBrowseVos)) {
                for (ActiveBrowseVo activeBrowseVo : activeBrowseVos) {
                    LocalDateTime browseTime = activeBrowseVo.getBrowseTime();
                    String mapKey = getMapKey(dateType, browseTime);
//                    String mapKey = LocalDateUtil.toLocalDateToString(browseTime.toLocalDate(), key);
                    List<ActiveBrowseVo> activeBrowseVoList = map.get(mapKey);
                    if (CollectionUtils.isEmpty(activeBrowseVoList)) {
                        activeBrowseVoList = new ArrayList<>();
                    }
                    activeBrowseVoList.add(activeBrowseVo);
                    map.put(mapKey, activeBrowseVoList);
                }
            }

            List<AllDataTrendStatisticsVo> allDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    int browseUserNum = 0;
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    List<ActiveBrowseVo> activeBrowseVoList = map.get(date);
                    if (!CollectionUtils.isEmpty(activeBrowseVoList)) {
                        browseUserNum = activeBrowseVoList.stream().map(ActiveBrowseVo::getUserId).collect(Collectors.toSet()).size();
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(browseUserNum));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(allDataTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(allDataTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    int browseUserNum = 0;
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    List<ActiveBrowseVo> activeBrowseVoList = map.get(date);
                    if (!CollectionUtils.isEmpty(activeBrowseVoList)) {
                        browseUserNum = activeBrowseVoList.stream().map(ActiveBrowseVo::getUserId).collect(Collectors.toSet()).size();
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(browseUserNum));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return allDataTrendStatisticsVos;
        } else if (type == 6) {
            // 浏览次数
            Map<String, Integer> map = new HashMap<>();

            ActiveBrowseStatisticsDto activeBrowseStatisticsDto = new ActiveBrowseStatisticsDto();
            activeBrowseStatisticsDto.setBusinessId(businessId);
            activeBrowseStatisticsDto.setStartCreateTime(allDataTrendStatisticsDto.getStartTime());
            activeBrowseStatisticsDto.setEndCreateTime(allDataTrendStatisticsDto.getEndTime());
            List<ActiveBrowseStatisticsVo> activeBrowseStatisticsVos = activeBrowseStatisticsApi.listByDto(activeBrowseStatisticsDto).unpack();
            if (!CollectionUtils.isEmpty(activeBrowseStatisticsVos)) {
                for (ActiveBrowseStatisticsVo activeBrowseVo : activeBrowseStatisticsVos) {
                    LocalDateTime browseTime = activeBrowseVo.getCreateTime();
                    String mapKey = getMapKey(dateType, browseTime);

                    Integer num = map.get(mapKey);
                    if (num == null) {
                        num = 0;
                    }
                    num += 1;
                    map.put(mapKey, num);
                }
            }

//            ActiveBrowseDto activeBrowseDto = new ActiveBrowseDto();
//            activeBrowseDto.setBusinessId(businessId);
//            activeBrowseDto.setStartBrowseTime(allDataTrendStatisticsDto.getStartTime());
//            activeBrowseDto.setEndBrowseTime(allDataTrendStatisticsDto.getEndTime());
//            List<ActiveBrowseVo> activeBrowseVos = activeBrowseApi.listByDto(activeBrowseDto).unpack();
//            if (!CollectionUtils.isEmpty(activeBrowseVos)) {
//                for (ActiveBrowseVo activeBrowseVo : activeBrowseVos) {
//                    LocalDateTime browseTime = activeBrowseVo.getBrowseTime();
//                    String mapKey = getMapKey(dateType, browseTime);
////                    String mapKey = LocalDateUtil.toLocalDateToString(browseTime.toLocalDate(), key);
//                    Integer num = map.get(mapKey);
//                    if (num == null) {
//                        num = 0;
//                    }
//                    num += 1;
//                    map.put(mapKey, num);
//                }
//            }


            List<AllDataTrendStatisticsVo> allDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    Integer num = map.get(date);
                    if (num == null) {
                        num = 0;
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(num));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(allDataTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(allDataTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    Integer num = map.get(date);
                    if (num == null) {
                        num = 0;
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(num));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return allDataTrendStatisticsVos;
        } else if (type == 7) {
            // 下单新用户
            Map<String, List<OrderFirstRecordDto>> map = new HashMap<>();

            OrderFirstRecordDto orderFirstRecordDto = new OrderFirstRecordDto();
            orderFirstRecordDto.setBusinessId(businessId);
            orderFirstRecordDto.setStartOrderTime(allDataTrendStatisticsDto.getStartTime());
            orderFirstRecordDto.setEndOrderTime(allDataTrendStatisticsDto.getEndTime());
            List<OrderFirstRecordVo> orderFirstRecordVos = orderFirstRecordApi.listByDto(orderFirstRecordDto).unpack();
            if (!CollectionUtils.isEmpty(orderFirstRecordVos)) {
                for (OrderFirstRecordVo orderFirstRecordVo : orderFirstRecordVos) {
                    LocalDateTime orderTime = orderFirstRecordVo.getOrderTime();
                    String mapKey = getMapKey(dateType, orderTime);
//                    String mapKey = LocalDateUtil.toLocalDateToString(orderTime.toLocalDate(), key);
                    List<OrderFirstRecordDto> orderFirstRecordDtos = map.get(mapKey);
                    if (CollectionUtils.isEmpty(orderFirstRecordDtos)) {
                        orderFirstRecordDtos = new ArrayList<>();
                    }
                    orderFirstRecordDtos.add(orderFirstRecordDto);
                    map.put(mapKey, orderFirstRecordDtos);
                }
            }


            List<AllDataTrendStatisticsVo> allDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    int newOrderUserNum = 0;
                    List<OrderFirstRecordDto> orderFirstRecordDtos = map.get(date);
                    if (!CollectionUtils.isEmpty(orderFirstRecordDtos)) {
                        newOrderUserNum = orderFirstRecordVos.stream().map(OrderFirstRecordVo::getUserId).collect(Collectors.toSet()).size();
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(newOrderUserNum));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(allDataTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(allDataTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    int newOrderUserNum = 0;
                    List<OrderFirstRecordDto> orderFirstRecordDtos = map.get(date);
                    if (!CollectionUtils.isEmpty(orderFirstRecordDtos)) {
                        newOrderUserNum = orderFirstRecordVos.stream().map(OrderFirstRecordVo::getUserId).collect(Collectors.toSet()).size();
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(newOrderUserNum));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return allDataTrendStatisticsVos;
        } else if (type == 8) {
            // 退款金额
            Map<String, BigDecimal> map = new HashMap<>();

            // 根据日期时间查询商家所有的订单数据,获取总订单金额（实付金额）,总订单数,总下单人数,总补贴金额,退款金额
            OrderDto orderDto = new OrderDto();
            orderDto.setBusinessId(businessId);
            orderDto.setStartOrderTime(allDataTrendStatisticsDto.getStartTime());
            orderDto.setEndOrderTime(allDataTrendStatisticsDto.getEndTime());
            List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
            if (!CollectionUtils.isEmpty(orderVos)) {
                for (OrderVo orderVo : orderVos) {
                    Integer orderState = orderVo.getOrderState();
                    if (OrderStateEnum.CANCEL.getValue().equals(orderState)) {
                        // 实付金额
                        BigDecimal payPrice = orderVo.getPayPrice();

                        // 下单时间
                        LocalDateTime orderTime = orderVo.getOrderTime();
                        String mapKey = getMapKey(dateType, orderTime);
                        BigDecimal amt = map.get(mapKey);
                        if (amt == null) {
                            amt = BigDecimal.ZERO;
                        }
                        amt = amt.add(payPrice);
                        map.put(mapKey, amt);
                    }
                }
            }


//            OrderItemDto orderItemDto = new OrderItemDto();
//            orderItemDto.setBusinessId(businessId);
//            orderItemDto.setStartOrderTime(allDataTrendStatisticsDto.getStartTime());
//            orderItemDto.setEndOrderTime(allDataTrendStatisticsDto.getEndTime());
//            List<OrderItemVo> orderItemVos = orderItemApi.listByDto(orderItemDto).unpack();
//            if (!CollectionUtils.isEmpty(orderItemVos)) {
//                // 取消下单的订单项列表
//                List<OrderItemVo> orderItemVoList = orderItemVos.stream().filter(orderItemVo ->
//                        (SubOrderStateEnum.CANCEL.getValue().equals(orderItemVo.getSubOrderState())
//                                || SubOrderStateEnum.EXPIRED.getValue().equals(orderItemVo.getSubOrderState()))).collect(Collectors.toList());
//                if (!CollectionUtils.isEmpty(orderItemVoList)) {
//                    for (OrderItemVo orderItemVo : orderItemVoList) {
//                        // 商户订单本金(按比例分配的用户支付的订单款)
//                        BigDecimal merchantAmt = orderItemVo.getMerchantAmt();
//                        BigDecimal distributionOrderAmt = orderItemVo.getDistributionOrderAmt();
//                        // 下单时间
//                        LocalDateTime orderTime = orderItemVo.getOrderTime();
//                        String mapKey = getMapKey(dateType, orderTime);
////                        String mapKey = LocalDateUtil.toLocalDateToString(orderTime.toLocalDate(), key);
//                        BigDecimal amt = map.get(mapKey);
//                        if (amt == null) {
//                            amt = BigDecimal.ZERO;
//                        }
//                        amt = amt.add(distributionOrderAmt);
//                        map.put(mapKey, amt);
//                    }
//                }
//            }


            List<AllDataTrendStatisticsVo> allDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    BigDecimal amt = map.get(date);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(amt.toString());
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(allDataTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(allDataTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    BigDecimal amt = map.get(date);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(amt.toString());
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return allDataTrendStatisticsVos;
        }

        return null;
    }

    public AllDataTradeStatisticsVo getAllDataTrade(AllDataStatisticsDto allDataStatisticsDto) {
        Long businessId = allDataStatisticsDto.getBusinessId();
        if (businessId == null || businessId <= 0) {
            Long userId = allDataStatisticsDto.getUserId();
            if (userId == null || userId <= 0) {
                Throw.isBusinessException(ResponseEnums.USER_ACCESS_DENIED);
            }

            BusinessPageVo businessPageVo = businessApi.getByUserId(userId).unpack();
            if (businessPageVo == null) {
                Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
            }

            allDataStatisticsDto.setBusinessId(businessPageVo.getId());
        }

        // 日期类型：1-今日，2-本周，3-本月，4-自定义
        Integer dateType = allDataStatisticsDto.getDateType();
        if (dateType == null) {
            Throw.isBusinessException("请选择查询日期类型");
        }

        LocalDate nowLocalDate = LocalDate.now();
        if (dateType == 1) {
            String nowDateStr = LocalDateUtil.toLocalDateToString(nowLocalDate, Constant.YYYY_MM_DD);

            allDataStatisticsDto.setStartTime(nowDateStr + " 00:00:00");
            allDataStatisticsDto.setEndTime(nowDateStr + " 23:59:59");
        } else if (dateType == 2) {
            // 本周开始日期
            LocalDate monday = nowLocalDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            // 本周结束日期
            LocalDate sunday = nowLocalDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));

            String mondayStartStr = LocalDateUtil.toLocalDateToString(monday, Constant.YYYY_MM_DD);
            String sundayEndStr = LocalDateUtil.toLocalDateToString(sunday, Constant.YYYY_MM_DD);

            allDataStatisticsDto.setStartTime(mondayStartStr + " 00:00:00");
            allDataStatisticsDto.setEndTime(sundayEndStr + " 23:59:59");
        } else if (dateType == 3) {
            // 本月1号
            LocalDate firstDayOfMonth = nowLocalDate.with(TemporalAdjusters.firstDayOfMonth());
            // 本月最后一天
            LocalDate lastDayOfMonth = nowLocalDate.with(TemporalAdjusters.lastDayOfMonth());

            String firstDayOfMonthStartStr = LocalDateUtil.toLocalDateToString(firstDayOfMonth, Constant.YYYY_MM_DD);
            String lastDayOfMonthEndStr = LocalDateUtil.toLocalDateToString(lastDayOfMonth, Constant.YYYY_MM_DD);

            allDataStatisticsDto.setStartTime(firstDayOfMonthStartStr + " 00:00:00");
            allDataStatisticsDto.setEndTime(lastDayOfMonthEndStr + " 23:59:59");
        } else if (dateType == 4) {
            String startTime = allDataStatisticsDto.getStartTime();
            String endTime = allDataStatisticsDto.getEndTime();
            if (StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)) {
                Throw.isBusinessException("请选择查询日期");
            }

            LocalDate startLocalDate = null;
            try {
                startLocalDate = LocalDateUtil.toLocalDate(startTime, Constant.YYYY_MM_DD);
            } catch (Exception e) {
                Throw.isBusinessException("查询开始日期格式错误");
            }

            LocalDate endLocalDate = null;
            try {
                endLocalDate = LocalDateUtil.toLocalDate(endTime, Constant.YYYY_MM_DD);
            } catch (Exception e) {
                Throw.isBusinessException("查询结束日期格式错误");
            }

            // 自定义日期最长不超过3个月
            long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate);
            if (days > 90) {
                Throw.isBusinessException("自定义日期最长不超过90天");
            }

            allDataStatisticsDto.setStartTime(startTime + " 00:00:00");
            allDataStatisticsDto.setEndTime(endTime + " 23:59:59");
        }

        // 总订单金额（实付金额）
        BigDecimal totalOrderAmt = BigDecimal.ZERO;
        // 总订单数
        int totalOrderNum = 0;
        // 总下单人数
        int totalOrderUserNum = 0;
        // 已结算补贴金额
        BigDecimal totalSubsidyAmt = BigDecimal.ZERO;
        // 退款金额
        BigDecimal refundOrderAmt = BigDecimal.ZERO;
        // 退款订单数
        int refundOrderNum = 0;
        // 已结算佣金
        BigDecimal totalCommissionAmt = BigDecimal.ZERO;

        // 根据日期时间查询商家所有的订单数据,获取总订单金额（实付金额）,总订单数,总下单人数,总补贴金额,退款金额
        OrderDto orderDto = new OrderDto();
        orderDto.setBusinessId(businessId);
        orderDto.setStartOrderTime(allDataStatisticsDto.getStartTime());
        orderDto.setEndOrderTime(allDataStatisticsDto.getEndTime());
        orderDto.setOrderStates(Arrays.asList(OrderStateEnum.WAIT_VERIFY.getValue(), OrderStateEnum.VERIFYING.getValue(),
                OrderStateEnum.FINISH.getValue(), OrderStateEnum.VERIFIED.getValue(), OrderStateEnum.CANCEL.getValue()));
        List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
        if (!CollectionUtils.isEmpty(orderVos)) {
            List<OrderVo> successOrderList = new ArrayList<>();
            for (OrderVo orderVo : orderVos) {
                Integer orderState = orderVo.getOrderState();
                if (OrderStateEnum.WAIT_VERIFY.getValue().equals(orderState) || OrderStateEnum.VERIFYING.getValue().equals(orderState)
                        || OrderStateEnum.FINISH.getValue().equals(orderState) || OrderStateEnum.VERIFIED.getValue().equals(orderState)) {
                    // 实付金额
                    BigDecimal payPrice = orderVo.getPayPrice();
                    // 总订单金额
                    totalOrderAmt = totalOrderAmt.add(payPrice);
                    // 总订单数
                    totalOrderNum += 1;

                    successOrderList.add(orderVo);
                } else if (OrderStateEnum.CANCEL.getValue().equals(orderState)) {
                    // 实付金额
                    BigDecimal payPrice = orderVo.getPayPrice();
                    // 总退款金额
                    refundOrderAmt = refundOrderAmt.add(payPrice);
                    refundOrderNum += 1;
                }
            }

            // 总下单人数
            if (!CollectionUtils.isEmpty(successOrderList)) {
                totalOrderUserNum = successOrderList.stream().map(OrderVo::getUserId).collect(Collectors.toSet()).size();
            }
        }

        OrderSettleBillDto orderSettleBillDto = new OrderSettleBillDto();
        orderSettleBillDto.setBusinessId(businessId);
        orderSettleBillDto.setStartTradeTime(allDataStatisticsDto.getStartTime());
        orderSettleBillDto.setEndTradeTime(allDataStatisticsDto.getEndTime());
        orderSettleBillDto.setTradeTypes(Arrays.asList(OrderSettleBillTradeTypeEnum.SUBSIDY_AMT.getValue(),
                OrderSettleBillTradeTypeEnum.COMMISSION_AMT.getValue()));
        List<OrderSettleBillVo> orderSettleBillVos = orderSettleBillApi.listByDto(orderSettleBillDto).unpack();
        if (!CollectionUtils.isEmpty(orderSettleBillVos)) {
            for (OrderSettleBillVo orderSettleBillVo : orderSettleBillVos) {
                totalSubsidyAmt = totalSubsidyAmt.add(orderSettleBillVo.getTradeAmt());
            }
        }

        AllDataTradeStatisticsVo allDataTradeStatisticsVo = new AllDataTradeStatisticsVo();
        allDataTradeStatisticsVo.setTotalOrderAmt(totalOrderAmt);
        allDataTradeStatisticsVo.setTotalOrderNum(totalOrderNum);
        allDataTradeStatisticsVo.setTotalOrderUserNum(totalOrderUserNum);
        allDataTradeStatisticsVo.setTotalSubsidyAmt(totalSubsidyAmt);
        allDataTradeStatisticsVo.setRefundOrderAmt(refundOrderAmt);
        allDataTradeStatisticsVo.setRefundOrderNum(refundOrderNum);
        allDataTradeStatisticsVo.setCommissionAmt(totalCommissionAmt);

        return allDataTradeStatisticsVo;
    }

    public List<AllDataTrendStatisticsVo> getAllDataTradeTrend(AllDataTradeTrendStatisticsDto allDataTradeTrendStatisticsDto) {
        Long businessId = allDataTradeTrendStatisticsDto.getBusinessId();
        if (businessId == null || businessId <= 0) {
            Long userId = allDataTradeTrendStatisticsDto.getUserId();
            if (userId == null || userId <= 0) {
                Throw.isBusinessException(ResponseEnums.USER_ACCESS_DENIED);
            }

            BusinessPageVo businessPageVo = businessApi.getByUserId(userId).unpack();
            if (businessPageVo == null) {
                Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
            }

            allDataTradeTrendStatisticsDto.setBusinessId(businessPageVo.getId());
        }

        // 类型：1-总订单金额，2-总订单数，3-总下单人数，4-已结算补贴金额，5-退款订单数，6-退款金额，7-已结算佣金
        Integer type = allDataTradeTrendStatisticsDto.getType();
        if (type == null) {
            Throw.isBusinessException("请选择查询类型");
        }

        // 日期类型：1-今日，2-本周，3-本月，4-自定义
        Integer dateType = allDataTradeTrendStatisticsDto.getDateType();
        if (dateType == null) {
            Throw.isBusinessException("请选择查询日期类型");
        }

        LocalDate nowLocalDate = LocalDate.now();
        if (dateType == 1) {
            String nowDateStr = LocalDateUtil.toLocalDateToString(nowLocalDate, Constant.YYYY_MM_DD);

            allDataTradeTrendStatisticsDto.setStartTime(nowDateStr + " 00:00:00");
            allDataTradeTrendStatisticsDto.setEndTime(nowDateStr + " 23:59:59");
        } else if (dateType == 2) {
            // 本周开始日期
            LocalDate monday = nowLocalDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            // 本周结束日期
            LocalDate sunday = nowLocalDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));

            String mondayStartStr = LocalDateUtil.toLocalDateToString(monday, Constant.YYYY_MM_DD);
            String sundayEndStr = LocalDateUtil.toLocalDateToString(sunday, Constant.YYYY_MM_DD);

            allDataTradeTrendStatisticsDto.setStartTime(mondayStartStr + " 00:00:00");
            allDataTradeTrendStatisticsDto.setEndTime(sundayEndStr + " 23:59:59");
        } else if (dateType == 3) {
            // 本月1号
            LocalDate firstDayOfMonth = nowLocalDate.with(TemporalAdjusters.firstDayOfMonth());
            // 本月最后一天
            LocalDate lastDayOfMonth = nowLocalDate.with(TemporalAdjusters.lastDayOfMonth());

            String firstDayOfMonthStartStr = LocalDateUtil.toLocalDateToString(firstDayOfMonth, Constant.YYYY_MM_DD);
            String lastDayOfMonthEndStr = LocalDateUtil.toLocalDateToString(lastDayOfMonth, Constant.YYYY_MM_DD);

            allDataTradeTrendStatisticsDto.setStartTime(firstDayOfMonthStartStr + " 00:00:00");
            allDataTradeTrendStatisticsDto.setEndTime(lastDayOfMonthEndStr + " 23:59:59");
        } else if (dateType == 4) {
            String startTime = allDataTradeTrendStatisticsDto.getStartTime();
            String endTime = allDataTradeTrendStatisticsDto.getEndTime();
            if (StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)) {
                Throw.isBusinessException("请选择查询日期");
            }

            LocalDate startLocalDate = null;
            try {
                startLocalDate = LocalDateUtil.toLocalDate(startTime, Constant.YYYY_MM_DD);
            } catch (Exception e) {
                Throw.isBusinessException("查询开始日期格式错误");
            }

            LocalDate endLocalDate = null;
            try {
                endLocalDate = LocalDateUtil.toLocalDate(endTime, Constant.YYYY_MM_DD);
            } catch (Exception e) {
                Throw.isBusinessException("查询结束日期格式错误");
            }

            // 自定义日期最长不超过3个月
            long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate);
            if (days > 90) {
                Throw.isBusinessException("自定义日期最长不超过90天");
            }

            allDataTradeTrendStatisticsDto.setStartTime(startTime + " 00:00:00");
            allDataTradeTrendStatisticsDto.setEndTime(endTime + " 23:59:59");
        }

        // 类型：1-总订单金额，2-总订单数，3-总下单人数，4-已结算补贴金额，5-退款订单数，6-退款金额，7-已结算佣金
        if (type == 1) {
            // 总订单金额
            Map<String, BigDecimal> map = new HashMap<>();

            // 根据日期时间查询订单数据，获取总订单金额（实付金额）
            OrderDto orderDto = new OrderDto();
            orderDto.setBusinessId(businessId);
            orderDto.setStartOrderTime(allDataTradeTrendStatisticsDto.getStartTime());
            orderDto.setEndOrderTime(allDataTradeTrendStatisticsDto.getEndTime());
            orderDto.setOrderStates(Arrays.asList(OrderStateEnum.WAIT_VERIFY.getValue(), OrderStateEnum.VERIFYING.getValue(),
                    OrderStateEnum.FINISH.getValue(), OrderStateEnum.VERIFIED.getValue()));
            List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
            if (!CollectionUtils.isEmpty(orderVos)) {
                for (OrderVo orderVo : orderVos) {
                    // 实付金额
                    BigDecimal payPrice = orderVo.getPayPrice();
                    // 下单时间
                    LocalDateTime orderTime = orderVo.getOrderTime();
                    String mapKey = getMapKey(dateType, orderTime);
                    BigDecimal amt = map.get(mapKey);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }
                    amt = amt.add(payPrice);
                    map.put(mapKey, amt);
                }
            }

            List<AllDataTrendStatisticsVo> allDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    BigDecimal amt = map.get(date);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(amt.toString());
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(allDataTradeTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(allDataTradeTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    BigDecimal amt = map.get(date);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(amt.toString());
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return allDataTrendStatisticsVos;
        } else if (type == 2) {
            // 总订单数
            Map<String, Integer> map = new HashMap<>();

            OrderDto orderDto = new OrderDto();
            orderDto.setBusinessId(businessId);
            orderDto.setStartOrderTime(allDataTradeTrendStatisticsDto.getStartTime());
            orderDto.setEndOrderTime(allDataTradeTrendStatisticsDto.getEndTime());
            orderDto.setOrderStates(Arrays.asList(OrderStateEnum.WAIT_VERIFY.getValue(), OrderStateEnum.VERIFYING.getValue(),
                    OrderStateEnum.FINISH.getValue(), OrderStateEnum.VERIFIED.getValue()));
            List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
            if (!CollectionUtils.isEmpty(orderVos)) {
                for (OrderVo orderVo : orderVos) {
                    // 下单时间
                    LocalDateTime orderTime = orderVo.getOrderTime();
                    String mapKey = getMapKey(dateType, orderTime);
//                        String mapKey = LocalDateUtil.toLocalDateToString(orderTime.toLocalDate(), key);
                    Integer count = map.get(mapKey);
                    if (count == null) {
                        count = 0;
                    }
                    count += 1;
                    map.put(mapKey, count);
                }
            }


//            OrderItemDto orderItemDto = new OrderItemDto();
//            orderItemDto.setBusinessId(businessId);
//            orderItemDto.setStartOrderTime(allDataTradeTrendStatisticsDto.getStartTime());
//            orderItemDto.setEndOrderTime(allDataTradeTrendStatisticsDto.getEndTime());
//            List<OrderItemVo> orderItemVos = orderItemApi.listByDto(orderItemDto).unpack();
//            if (!CollectionUtils.isEmpty(orderItemVos)) {
//                // 成功下单的订单项列表
//                List<OrderItemVo> orderItemVoList = orderItemVos.stream().filter(orderItemVo ->
//                        (SubOrderStateEnum.WAIT_VERIFY.getValue().equals(orderItemVo.getSubOrderState())
//                                || SubOrderStateEnum.VERIFIED.getValue().equals(orderItemVo.getSubOrderState()))).collect(Collectors.toList());
//                if (!CollectionUtils.isEmpty(orderItemVoList)) {
//                    for (OrderItemVo orderItemVo : orderItemVoList) {
//                        // 下单时间
//                        LocalDateTime orderTime = orderItemVo.getOrderTime();
//                        String mapKey = getMapKey(dateType, orderTime);
////                        String mapKey = LocalDateUtil.toLocalDateToString(orderTime.toLocalDate(), key);
//                        List<OrderItemVo> orderItems = map.get(mapKey);
//                        if (CollectionUtils.isEmpty(orderItems)) {
//                            orderItems = new ArrayList<>();
//                        }
//                        orderItems.add(orderItemVo);
//                        map.put(mapKey, orderItems);
//                    }
//                }
//            }

            List<AllDataTrendStatisticsVo> allDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    int orderNum = 0;
                    Integer count = map.get(date);
                    if (count != null) {
                        orderNum = count;
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(orderNum));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(allDataTradeTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(allDataTradeTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    int orderNum = 0;
                    Integer count = map.get(date);
                    if (count != null) {
                        orderNum = count;
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(orderNum));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return allDataTrendStatisticsVos;
        } else if (type == 3) {
            // 总下单人数
            Map<String, List<OrderVo>> map = new HashMap<>();

            OrderDto orderDto = new OrderDto();
            orderDto.setBusinessId(businessId);
            orderDto.setStartOrderTime(allDataTradeTrendStatisticsDto.getStartTime());
            orderDto.setEndOrderTime(allDataTradeTrendStatisticsDto.getEndTime());
            orderDto.setOrderStates(Arrays.asList(OrderStateEnum.WAIT_VERIFY.getValue(), OrderStateEnum.VERIFYING.getValue(),
                    OrderStateEnum.FINISH.getValue(), OrderStateEnum.VERIFIED.getValue()));
            List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
            if (!CollectionUtils.isEmpty(orderVos)) {
                for (OrderVo orderVo : orderVos) {
                    // 下单时间
                    LocalDateTime orderTime = orderVo.getOrderTime();
                    String mapKey = getMapKey(dateType, orderTime);
                    List<OrderVo> orderVoList = map.get(mapKey);
                    if (CollectionUtils.isEmpty(orderVoList)) {
                        orderVoList = new ArrayList<>();
                    }
                    map.put(mapKey, orderVoList);
                }
            }

            List<AllDataTrendStatisticsVo> allDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    int orderUserNum = 0;
                    List<OrderVo> orderItems = map.get(date);
                    if (!CollectionUtils.isEmpty(orderItems)) {
                        orderUserNum = orderItems.stream().map(OrderVo::getUserId).collect(Collectors.toSet()).size();
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(orderUserNum));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(allDataTradeTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(allDataTradeTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    int orderUserNum = 0;
                    List<OrderVo> orderItems = map.get(date);
                    if (!CollectionUtils.isEmpty(orderItems)) {
                        orderUserNum = orderItems.stream().map(OrderVo::getUserId).collect(Collectors.toSet()).size();
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(orderUserNum));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return allDataTrendStatisticsVos;
        } else if (type == 4) {
            // 已结算补贴金额
            Map<String, BigDecimal> map = new HashMap<>();

            OrderSettleBillDto orderSettleBillDto = new OrderSettleBillDto();
            orderSettleBillDto.setBusinessId(businessId);
            orderSettleBillDto.setStartTradeTime(allDataTradeTrendStatisticsDto.getStartTime());
            orderSettleBillDto.setEndTradeTime(allDataTradeTrendStatisticsDto.getEndTime());
            orderSettleBillDto.setTradeTypes(Arrays.asList(OrderSettleBillTradeTypeEnum.SUBSIDY_AMT.getValue(),
                    OrderSettleBillTradeTypeEnum.COMMISSION_AMT.getValue()));
            List<OrderSettleBillVo> orderSettleBillVos = orderSettleBillApi.listByDto(orderSettleBillDto).unpack();
            if (!CollectionUtils.isEmpty(orderSettleBillVos)) {
                for (OrderSettleBillVo orderSettleBillVo : orderSettleBillVos) {
                    LocalDateTime tradeTime = orderSettleBillVo.getTradeTime();
                    BigDecimal tradeAmt = orderSettleBillVo.getTradeAmt();
                    String mapKey = getMapKey(dateType, tradeTime);
                    BigDecimal amt = map.get(mapKey);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }
                    amt = amt.add(tradeAmt);
                    map.put(mapKey, amt);
                }
            }

            List<AllDataTrendStatisticsVo> allDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    BigDecimal amt = map.get(date);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(amt.toString());
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(allDataTradeTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(allDataTradeTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    BigDecimal amt = map.get(date);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(amt.toString());
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return allDataTrendStatisticsVos;
        } else if (type == 5) {
            // 退款订单数
            Map<String, Integer> map = new HashMap<>();

            OrderDto orderDto = new OrderDto();
            orderDto.setBusinessId(businessId);
            orderDto.setStartOrderTime(allDataTradeTrendStatisticsDto.getStartTime());
            orderDto.setEndOrderTime(allDataTradeTrendStatisticsDto.getEndTime());
            orderDto.setOrderStates(Collections.singletonList(OrderStateEnum.CANCEL.getValue()));
            List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
            if (!CollectionUtils.isEmpty(orderVos)) {
                for (OrderVo orderVo : orderVos) {
                    // 下单时间
                    LocalDateTime orderTime = orderVo.getOrderTime();
                    String mapKey = getMapKey(dateType, orderTime);
//                        String mapKey = LocalDateUtil.toLocalDateToString(orderTime.toLocalDate(), key);
                    Integer count = map.get(mapKey);
                    if (count == null) {
                        count = 0;
                    }
                    count += 1;
                    map.put(mapKey, count);
                }
            }

//            OrderItemDto orderItemDto = new OrderItemDto();
//            orderItemDto.setBusinessId(businessId);
//            orderItemDto.setStartOrderTime(allDataTradeTrendStatisticsDto.getStartTime());
//            orderItemDto.setEndOrderTime(allDataTradeTrendStatisticsDto.getEndTime());
//            List<OrderItemVo> orderItemVos = orderItemApi.listByDto(orderItemDto).unpack();
//            if (!CollectionUtils.isEmpty(orderItemVos)) {
//                // 取消下单的订单项列表
//                List<OrderItemVo> orderItemVoList = orderItemVos.stream().filter(orderItemVo ->
//                        (SubOrderStateEnum.CANCEL.getValue().equals(orderItemVo.getSubOrderState())
//                                || SubOrderStateEnum.EXPIRED.getValue().equals(orderItemVo.getSubOrderState()))).collect(Collectors.toList());
//                if (!CollectionUtils.isEmpty(orderItemVoList)) {
//                    for (OrderItemVo orderItemVo : orderItemVoList) {
//                        // 下单时间
//                        LocalDateTime orderTime = orderItemVo.getOrderTime();
//                        String mapKey = getMapKey(dateType, orderTime);
////                        String mapKey = LocalDateUtil.toLocalDateToString(orderTime.toLocalDate(), key);
//                        List<OrderItemVo> orderItems = map.get(mapKey);
//                        if (CollectionUtils.isEmpty(orderItems)) {
//                            orderItems = new ArrayList<>();
//                        }
//                        orderItems.add(orderItemVo);
//                        map.put(mapKey, orderItems);
//                    }
//                }
//            }


            List<AllDataTrendStatisticsVo> allDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    int orderNum = 0;
                    Integer count = map.get(date);
                    if (count != null) {
                        orderNum = count;
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(orderNum));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(allDataTradeTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(allDataTradeTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    int orderNum = 0;
                    Integer count = map.get(date);
                    if (count != null) {
                        orderNum = count;
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(orderNum));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return allDataTrendStatisticsVos;
        } else if (type == 6) {
            // 退款金额
            Map<String, BigDecimal> map = new HashMap<>();

            OrderDto orderDto = new OrderDto();
            orderDto.setBusinessId(businessId);
            orderDto.setStartOrderTime(allDataTradeTrendStatisticsDto.getStartTime());
            orderDto.setEndOrderTime(allDataTradeTrendStatisticsDto.getEndTime());
            orderDto.setOrderStates(Collections.singletonList(OrderStateEnum.CANCEL.getValue()));
            List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
            if (!CollectionUtils.isEmpty(orderVos)) {
                for (OrderVo orderVo : orderVos) {
                    // 下单时间
                    LocalDateTime orderTime = orderVo.getOrderTime();
                    String mapKey = getMapKey(dateType, orderTime);
                    BigDecimal amt = map.get(mapKey);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }
                    amt = amt.add(orderVo.getPayPrice());
                    map.put(mapKey, amt);
                }
            }

//            OrderItemDto orderItemDto = new OrderItemDto();
//            orderItemDto.setBusinessId(businessId);
//            orderItemDto.setStartOrderTime(allDataTradeTrendStatisticsDto.getStartTime());
//            orderItemDto.setEndOrderTime(allDataTradeTrendStatisticsDto.getEndTime());
//            List<OrderItemVo> orderItemVos = orderItemApi.listByDto(orderItemDto).unpack();
//            if (!CollectionUtils.isEmpty(orderItemVos)) {
//                // 取消下单的订单项列表
//                List<OrderItemVo> orderItemVoList = orderItemVos.stream().filter(orderItemVo ->
//                        (SubOrderStateEnum.CANCEL.getValue().equals(orderItemVo.getSubOrderState())
//                                || SubOrderStateEnum.EXPIRED.getValue().equals(orderItemVo.getSubOrderState()))).collect(Collectors.toList());
//                if (!CollectionUtils.isEmpty(orderItemVoList)) {
//                    for (OrderItemVo orderItemVo : orderItemVoList) {
//                        // 商户订单本金(按比例分配的用户支付的订单款)
//                        BigDecimal merchantAmt = orderItemVo.getMerchantAmt();
//                        BigDecimal distributionOrderAmt = orderItemVo.getDistributionOrderAmt();
//                        // 下单时间
//                        LocalDateTime orderTime = orderItemVo.getOrderTime();
//                        String mapKey = getMapKey(dateType, orderTime);
////                        String mapKey = LocalDateUtil.toLocalDateToString(orderTime.toLocalDate(), key);
//                        BigDecimal amt = map.get(mapKey);
//                        if (amt == null) {
//                            amt = BigDecimal.ZERO;
//                        }
//                        amt = amt.add(distributionOrderAmt);
//                        map.put(mapKey, amt);
//                    }
//                }
//            }


            List<AllDataTrendStatisticsVo> allDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    BigDecimal amt = map.get(date);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(amt.toString());
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(allDataTradeTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(allDataTradeTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    BigDecimal amt = map.get(date);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(amt.toString());
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return allDataTrendStatisticsVos;
        } else if (type == 7) {
            // 已结算佣金
            Map<String, BigDecimal> map = new HashMap<>();

            OrderDto orderDto = new OrderDto();
            orderDto.setBusinessId(businessId);
            orderDto.setStartOrderTime(allDataTradeTrendStatisticsDto.getStartTime());
            orderDto.setEndOrderTime(allDataTradeTrendStatisticsDto.getEndTime());
            orderDto.setOrderStates(Collections.singletonList(OrderStateEnum.FINISH.getValue()));
            List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
            if (!CollectionUtils.isEmpty(orderVos)) {
                List<Long> orderIds = orderVos.stream().map(OrderVo::getId).collect(Collectors.toList());

                Map<Long, List<OrderItemVo>> orderItemVoMap = new HashMap<>();
                OrderItemDto orderItemDto = new OrderItemDto();
                orderItemDto.setBusinessId(businessId);
                orderItemDto.setOrderIds(orderIds);
                orderItemDto.setSubOrderStates(Collections.singletonList(SubOrderStateEnum.VERIFIED.getValue()));
                List<OrderItemVo> orderItemVos = orderItemApi.listByDto(orderItemDto).unpack();
                if (!CollectionUtils.isEmpty(orderItemVos)) {
                    orderItemVoMap = orderItemVos.stream().collect(Collectors.groupingBy(OrderItemVo::getOrderId));
                }

                for (OrderVo orderVo : orderVos) {
                    List<OrderItemVo> orderItemVoList = orderItemVoMap.get(orderVo.getId());
                    if (!CollectionUtils.isEmpty(orderItemVoList)) {
                        for (OrderItemVo orderItemVo : orderItemVoList) {
                            // 佣金
                            BigDecimal commissionAmt = orderItemVo.getCommissionAmt();
                            // 下单时间
                            LocalDateTime orderTime = orderItemVo.getOrderTime();
                            String mapKey = getMapKey(dateType, orderTime);
                            BigDecimal amt = map.get(mapKey);
                            if (amt == null) {
                                amt = BigDecimal.ZERO;
                            }
                            amt = amt.add(commissionAmt);
                            map.put(mapKey, amt);
                        }
                    }
                }
            }


//            OrderItemDto orderItemDto = new OrderItemDto();
//            orderItemDto.setBusinessId(businessId);
//            orderItemDto.setStartOrderTime(allDataTradeTrendStatisticsDto.getStartTime());
//            orderItemDto.setEndOrderTime(allDataTradeTrendStatisticsDto.getEndTime());
//            List<OrderItemVo> orderItemVos = orderItemApi.listByDto(orderItemDto).unpack();
//            if (!CollectionUtils.isEmpty(orderItemVos)) {
//                // 已结算的订单项列表
//                List<OrderItemVo> orderItemVoList = orderItemVos.stream().filter(orderItemVo ->
//                        SettleStateEnum.YES.getValue().equals(orderItemVo.getSettleState())).collect(Collectors.toList());
////                List<OrderItemVo> orderItemVoList = orderItemVos.stream().filter(orderItemVo ->
////                        (SubOrderStateEnum.WAIT_VERIFY.getValue().equals(orderItemVo.getSubOrderState())
////                                || SubOrderStateEnum.VERIFIED.getValue().equals(orderItemVo.getSubOrderState()))).collect(Collectors.toList());
//                if (!CollectionUtils.isEmpty(orderItemVoList)) {
//                    for (OrderItemVo orderItemVo : orderItemVoList) {
//                        // 佣金
//                        BigDecimal commissionAmt = orderItemVo.getCommissionAmt();
//                        // 下单时间
//                        LocalDateTime orderTime = orderItemVo.getOrderTime();
//                        String mapKey = getMapKey(dateType, orderTime);
////                        String mapKey = LocalDateUtil.toLocalDateToString(orderTime.toLocalDate(), key);
//                        BigDecimal amt = map.get(mapKey);
//                        if (amt == null) {
//                            amt = BigDecimal.ZERO;
//                        }
//                        amt = amt.add(commissionAmt);
//                        map.put(mapKey, amt);
//                    }
//                }
//            }


            List<AllDataTrendStatisticsVo> allDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    BigDecimal amt = map.get(date);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(amt.toString());
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(allDataTradeTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(allDataTradeTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    BigDecimal amt = map.get(date);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(amt.toString());
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return allDataTrendStatisticsVos;
        }

        return null;
    }

    public AllDataUserStatisticsVo getAllDataUser(AllDataStatisticsDto allDataStatisticsDto) {
        Long businessId = allDataStatisticsDto.getBusinessId();
        if (businessId == null || businessId <= 0) {
            Long userId = allDataStatisticsDto.getUserId();
            if (userId == null || userId <= 0) {
                Throw.isBusinessException(ResponseEnums.USER_ACCESS_DENIED);
            }

            BusinessPageVo businessPageVo = businessApi.getByUserId(userId).unpack();
            if (businessPageVo == null) {
                Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
            }

            allDataStatisticsDto.setBusinessId(businessPageVo.getId());
        }

        // 日期类型：1-今日，2-本周，3-本月，4-自定义
        Integer dateType = allDataStatisticsDto.getDateType();
        if (dateType == null) {
            Throw.isBusinessException("请选择查询日期类型");
        }

        LocalDate nowLocalDate = LocalDate.now();
        if (dateType == 1) {
            String nowDateStr = LocalDateUtil.toLocalDateToString(nowLocalDate, Constant.YYYY_MM_DD);

            allDataStatisticsDto.setStartTime(nowDateStr + " 00:00:00");
            allDataStatisticsDto.setEndTime(nowDateStr + " 23:59:59");
        } else if (dateType == 2) {
            // 本周开始日期
            LocalDate monday = nowLocalDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            // 本周结束日期
            LocalDate sunday = nowLocalDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));

            String mondayStartStr = LocalDateUtil.toLocalDateToString(monday, Constant.YYYY_MM_DD);
            String sundayEndStr = LocalDateUtil.toLocalDateToString(sunday, Constant.YYYY_MM_DD);

            allDataStatisticsDto.setStartTime(mondayStartStr + " 00:00:00");
            allDataStatisticsDto.setEndTime(sundayEndStr + " 23:59:59");
        } else if (dateType == 3) {
            // 本月1号
            LocalDate firstDayOfMonth = nowLocalDate.with(TemporalAdjusters.firstDayOfMonth());
            // 本月最后一天
            LocalDate lastDayOfMonth = nowLocalDate.with(TemporalAdjusters.lastDayOfMonth());

            String firstDayOfMonthStartStr = LocalDateUtil.toLocalDateToString(firstDayOfMonth, Constant.YYYY_MM_DD);
            String lastDayOfMonthEndStr = LocalDateUtil.toLocalDateToString(lastDayOfMonth, Constant.YYYY_MM_DD);

            allDataStatisticsDto.setStartTime(firstDayOfMonthStartStr + " 00:00:00");
            allDataStatisticsDto.setEndTime(lastDayOfMonthEndStr + " 23:59:59");
        } else if (dateType == 4) {
            String startTime = allDataStatisticsDto.getStartTime();
            String endTime = allDataStatisticsDto.getEndTime();
            if (StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)) {
                Throw.isBusinessException("请选择查询日期");
            }

            LocalDate startLocalDate = null;
            try {
                startLocalDate = LocalDateUtil.toLocalDate(startTime, Constant.YYYY_MM_DD);
            } catch (Exception e) {
                Throw.isBusinessException("查询开始日期格式错误");
            }

            LocalDate endLocalDate = null;
            try {
                endLocalDate = LocalDateUtil.toLocalDate(endTime, Constant.YYYY_MM_DD);
            } catch (Exception e) {
                Throw.isBusinessException("查询结束日期格式错误");
            }

            // 自定义日期最长不超过3个月
            long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate);
            if (days > 90) {
                Throw.isBusinessException("自定义日期最长不超过90天");
            }

            allDataStatisticsDto.setStartTime(startTime + " 00:00:00");
            allDataStatisticsDto.setEndTime(endTime + " 23:59:59");
        }

        // 总下单人数
        int totalOrderUserNum = 0;
        // 浏览人数
        int browseUserNum = 0;
        // 浏览次数
        int browseNum = 0;
        // 分享人数
        int shareUserNum = 0;
        // 新增订阅人数
        int newSubscribeUserNum = 0;
        // 累计订阅人数
        int totalSubscribeUserNum = 0;

        // 根据日期时间查询订单项数据，获取总下单人数
        OrderDto orderDto = new OrderDto();
        orderDto.setBusinessId(businessId);
        orderDto.setStartOrderTime(allDataStatisticsDto.getStartTime());
        orderDto.setEndOrderTime(allDataStatisticsDto.getEndTime());
        orderDto.setOrderStates(Arrays.asList(OrderStateEnum.WAIT_VERIFY.getValue(), OrderStateEnum.VERIFYING.getValue(),
                OrderStateEnum.FINISH.getValue(), OrderStateEnum.VERIFIED.getValue()));
        List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
        if (!CollectionUtils.isEmpty(orderVos)) {
            // 总下单人数
            totalOrderUserNum = orderVos.size();
        }

//        OrderItemDto orderItemDto = new OrderItemDto();
//        orderItemDto.setBusinessId(businessId);
//        orderItemDto.setStartOrderTime(allDataStatisticsDto.getStartTime());
//        orderItemDto.setEndOrderTime(allDataStatisticsDto.getEndTime());
//        List<OrderItemVo> orderItemVos = orderItemApi.listByDto(orderItemDto).unpack();
//        if (!CollectionUtils.isEmpty(orderItemVos)) {
//            // 成功下单的订单项列表
//            List<OrderItemVo> orderItemVoList = orderItemVos.stream().filter(orderItemVo ->
//                    (SubOrderStateEnum.WAIT_VERIFY.getValue().equals(orderItemVo.getSubOrderState())
//                            || SubOrderStateEnum.VERIFIED.getValue().equals(orderItemVo.getSubOrderState()))).collect(Collectors.toList());
//            if (!CollectionUtils.isEmpty(orderItemVoList)) {
//                // 总下单人数
//                totalOrderUserNum = orderItemVoList.stream().map(OrderItemVo::getUserId).collect(Collectors.toSet()).size();
//            }
//        }

        // 根据日期查询浏览数据，获取浏览人数，浏览次数
        ActiveBrowseDto activeBrowseDto = new ActiveBrowseDto();
        activeBrowseDto.setBusinessId(businessId);
        activeBrowseDto.setStartBrowseTime(allDataStatisticsDto.getStartTime());
        activeBrowseDto.setEndBrowseTime(allDataStatisticsDto.getEndTime());
        List<ActiveBrowseVo> activeBrowseVos = activeBrowseApi.listByDto(activeBrowseDto).unpack();
        if (!CollectionUtils.isEmpty(activeBrowseVos)) {
            browseUserNum = activeBrowseVos.stream().map(ActiveBrowseVo::getUserId).collect(Collectors.toSet()).size();
        }

        ActiveBrowseStatisticsDto activeBrowseStatisticsDto = new ActiveBrowseStatisticsDto();
        activeBrowseStatisticsDto.setBusinessId(businessId);
        activeBrowseStatisticsDto.setStartCreateTime(allDataStatisticsDto.getStartTime());
        activeBrowseStatisticsDto.setEndCreateTime(allDataStatisticsDto.getEndTime());
        List<ActiveBrowseStatisticsVo> activeBrowseStatisticsVos = activeBrowseStatisticsApi.listByDto(activeBrowseStatisticsDto).unpack();
        if (!CollectionUtils.isEmpty(activeBrowseStatisticsVos)) {
            for (ActiveBrowseStatisticsVo activeBrowseVo : activeBrowseStatisticsVos) {
                browseNum = browseNum + Integer.parseInt(String.valueOf(activeBrowseVo.getBrowseNum()));
            }
        }

        // 根据日期查询活动分享数据，获取分享人数
        ActiveShareStatisticsDto activeShareStatisticsDto = new ActiveShareStatisticsDto();
        activeShareStatisticsDto.setBusinessId(businessId);
        activeShareStatisticsDto.setStartShareTime(allDataStatisticsDto.getStartTime());
        activeShareStatisticsDto.setEndShareTime(allDataStatisticsDto.getEndTime());
        List<ActiveShareStatisticsVo> activeShareStatisticsVos = activeShareApi.listByDto(activeShareStatisticsDto).unpack();
        if (!CollectionUtils.isEmpty(activeShareStatisticsVos)) {
            shareUserNum = activeShareStatisticsVos.stream().map(ActiveShareStatisticsVo::getUserId).collect(Collectors.toSet()).size();
        }

        // 查询订阅商家人数
        List<BusinessSubscribeVo> businessSubscribeVos = businessSubscribeApi.listByBusinessIds(Collections.singletonList(businessId)).unpack();
        if (!CollectionUtils.isEmpty(businessSubscribeVos)) {
            // 总订阅人数
            totalSubscribeUserNum = businessSubscribeVos.size();

            LocalDateTime startLocalDateTime = LocalDateUtil.toLocalDateTime(allDataStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS);
            LocalDateTime endLocalDateTime = LocalDateUtil.toLocalDateTime(allDataStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS);
            // 新增订阅人数
            for (BusinessSubscribeVo businessSubscribeVo : businessSubscribeVos) {
                LocalDateTime createTime = businessSubscribeVo.getCreateTime();
                if (createTime.isAfter(startLocalDateTime) && createTime.isBefore(endLocalDateTime)) {
                    newSubscribeUserNum += 1;
                }
            }
        }

        AllDataUserStatisticsVo allDataUserStatisticsVo = new AllDataUserStatisticsVo();
        allDataUserStatisticsVo.setTotalOrderUserNum(totalOrderUserNum);
        allDataUserStatisticsVo.setBrowseUserNum(browseUserNum);
        allDataUserStatisticsVo.setBrowseNum(browseNum);
        allDataUserStatisticsVo.setShareUserNum(shareUserNum);
        allDataUserStatisticsVo.setNewSubscribeUserNum(newSubscribeUserNum);
        allDataUserStatisticsVo.setTotalSubscribeUserNum(totalSubscribeUserNum);

        return allDataUserStatisticsVo;
    }

    public List<AllDataTrendStatisticsVo> getAllDataUserTrend(AllDataUserTrendStatisticsDto allDataUserTrendStatisticsDto) {
        Long businessId = allDataUserTrendStatisticsDto.getBusinessId();
        if (businessId == null || businessId <= 0) {
            Long userId = allDataUserTrendStatisticsDto.getUserId();
            if (userId == null || userId <= 0) {
                Throw.isBusinessException(ResponseEnums.USER_ACCESS_DENIED);
            }

            BusinessPageVo businessPageVo = businessApi.getByUserId(userId).unpack();
            if (businessPageVo == null) {
                Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
            }

            allDataUserTrendStatisticsDto.setBusinessId(businessPageVo.getId());
        }

        // 类型：1-总下单人数，2-浏览人数，3-浏览次数，4-分享人数，5-新增订阅人数，6-累计订阅人数
        Integer type = allDataUserTrendStatisticsDto.getType();
        if (type == null) {
            Throw.isBusinessException("请选择查询类型");
        }

        // 日期类型：1-今日，2-本周，3-本月，4-自定义
        Integer dateType = allDataUserTrendStatisticsDto.getDateType();
        if (dateType == null) {
            Throw.isBusinessException("请选择查询日期类型");
        }

        LocalDate nowLocalDate = LocalDate.now();
        if (dateType == 1) {
            String nowDateStr = LocalDateUtil.toLocalDateToString(nowLocalDate, Constant.YYYY_MM_DD);

            allDataUserTrendStatisticsDto.setStartTime(nowDateStr + " 00:00:00");
            allDataUserTrendStatisticsDto.setEndTime(nowDateStr + " 23:59:59");
        } else if (dateType == 2) {
            // 本周开始日期
            LocalDate monday = nowLocalDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            // 本周结束日期
            LocalDate sunday = nowLocalDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));

            String mondayStartStr = LocalDateUtil.toLocalDateToString(monday, Constant.YYYY_MM_DD);
            String sundayEndStr = LocalDateUtil.toLocalDateToString(sunday, Constant.YYYY_MM_DD);

            allDataUserTrendStatisticsDto.setStartTime(mondayStartStr + " 00:00:00");
            allDataUserTrendStatisticsDto.setEndTime(sundayEndStr + " 23:59:59");
        } else if (dateType == 3) {
            // 本月1号
            LocalDate firstDayOfMonth = nowLocalDate.with(TemporalAdjusters.firstDayOfMonth());
            // 本月最后一天
            LocalDate lastDayOfMonth = nowLocalDate.with(TemporalAdjusters.lastDayOfMonth());

            String firstDayOfMonthStartStr = LocalDateUtil.toLocalDateToString(firstDayOfMonth, Constant.YYYY_MM_DD);
            String lastDayOfMonthEndStr = LocalDateUtil.toLocalDateToString(lastDayOfMonth, Constant.YYYY_MM_DD);

            allDataUserTrendStatisticsDto.setStartTime(firstDayOfMonthStartStr + " 00:00:00");
            allDataUserTrendStatisticsDto.setEndTime(lastDayOfMonthEndStr + " 23:59:59");
        } else if (dateType == 4) {
            String startTime = allDataUserTrendStatisticsDto.getStartTime();
            String endTime = allDataUserTrendStatisticsDto.getEndTime();
            if (StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)) {
                Throw.isBusinessException("请选择查询日期");
            }

            LocalDate startLocalDate = null;
            try {
                startLocalDate = LocalDateUtil.toLocalDate(startTime, Constant.YYYY_MM_DD);
            } catch (Exception e) {
                Throw.isBusinessException("查询开始日期格式错误");
            }

            LocalDate endLocalDate = null;
            try {
                endLocalDate = LocalDateUtil.toLocalDate(endTime, Constant.YYYY_MM_DD);
            } catch (Exception e) {
                Throw.isBusinessException("查询结束日期格式错误");
            }

            // 自定义日期最长不超过3个月
            long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate);
            if (days > 90) {
                Throw.isBusinessException("自定义日期最长不超过90天");
            }

            allDataUserTrendStatisticsDto.setStartTime(startTime + " 00:00:00");
            allDataUserTrendStatisticsDto.setEndTime(endTime + " 23:59:59");
        }

        //  今日 24小时  3或4小时一条
        //  本周  7天  按天
        //  其他日期按天（只能选到天））  最大不超过3个月
        // 根据日期类型判断是根据小时分组还是日期
//        String key = dateType == 1 ? "HH" : "yyyy-MM-dd";

        // 类型：1-总下单人数，2-浏览人数，3-浏览次数，4-分享人数，5-新增订阅人数，6-累计订阅人数
        if (type == 1) {
            // 总下单人数
            Map<String, List<OrderVo>> map = new HashMap<>();

            OrderDto orderDto = new OrderDto();
            orderDto.setBusinessId(businessId);
            orderDto.setStartOrderTime(allDataUserTrendStatisticsDto.getStartTime());
            orderDto.setEndOrderTime(allDataUserTrendStatisticsDto.getEndTime());
            orderDto.setOrderStates(Arrays.asList(OrderStateEnum.WAIT_VERIFY.getValue(), OrderStateEnum.VERIFYING.getValue(),
                    OrderStateEnum.FINISH.getValue(), OrderStateEnum.VERIFIED.getValue()));
            List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
            if (!CollectionUtils.isEmpty(orderVos)) {
                for (OrderVo orderVo : orderVos) {
                    // 下单时间
                    LocalDateTime orderTime = orderVo.getOrderTime();
                    String mapKey = getMapKey(dateType, orderTime);
//                        String mapKey = LocalDateUtil.toLocalDateToString(orderTime.toLocalDate(), key);
                    List<OrderVo> orderVoList = map.get(mapKey);
                    if (CollectionUtils.isEmpty(orderVoList)) {
                        orderVoList = new ArrayList<>();
                    }
                    map.put(mapKey, orderVoList);
                }
            }

//            OrderItemDto orderItemDto = new OrderItemDto();
//            orderItemDto.setBusinessId(businessId);
//            orderItemDto.setStartOrderTime(allDataUserTrendStatisticsDto.getStartTime());
//            orderItemDto.setEndOrderTime(allDataUserTrendStatisticsDto.getEndTime());
//            List<OrderItemVo> orderItemVos = orderItemApi.listByDto(orderItemDto).unpack();
//            if (!CollectionUtils.isEmpty(orderItemVos)) {
//                // 成功下单的订单项列表
//                List<OrderItemVo> orderItemVoList = orderItemVos.stream().filter(orderItemVo ->
//                        (SubOrderStateEnum.WAIT_VERIFY.getValue().equals(orderItemVo.getSubOrderState())
//                                || SubOrderStateEnum.VERIFIED.getValue().equals(orderItemVo.getSubOrderState()))).collect(Collectors.toList());
//                if (!CollectionUtils.isEmpty(orderItemVoList)) {
//                    for (OrderItemVo orderItemVo : orderItemVoList) {
//                        // 下单时间
//                        LocalDateTime orderTime = orderItemVo.getOrderTime();
//                        String mapKey = getMapKey(dateType, orderTime);
////                        String mapKey = LocalDateUtil.toLocalDateToString(orderTime.toLocalDate(), key);
//                        List<OrderItemVo> orderItems = map.get(mapKey);
//                        if (CollectionUtils.isEmpty(orderItems)) {
//                            orderItems = new ArrayList<>();
//                        }
//                        orderItems.add(orderItemVo);
//                        map.put(mapKey, orderItems);
//                    }
//                }
//            }


            List<AllDataTrendStatisticsVo> allDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    int orderUserNum = 0;
                    List<OrderVo> orderItems = map.get(date);
                    if (!CollectionUtils.isEmpty(orderItems)) {
                        orderUserNum = orderItems.stream().map(OrderVo::getUserId).collect(Collectors.toSet()).size();
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(orderUserNum));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(allDataUserTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(allDataUserTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    int orderUserNum = 0;
                    List<OrderVo> orderItems = map.get(date);
                    if (!CollectionUtils.isEmpty(orderItems)) {
                        orderUserNum = orderItems.stream().map(OrderVo::getUserId).collect(Collectors.toSet()).size();
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(orderUserNum));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return allDataTrendStatisticsVos;
        } else if (type == 2) {
            // 浏览人数
            Map<String, List<ActiveBrowseVo>> map = new HashMap<>();

            ActiveBrowseDto activeBrowseDto = new ActiveBrowseDto();
            activeBrowseDto.setBusinessId(businessId);
            activeBrowseDto.setStartBrowseTime(allDataUserTrendStatisticsDto.getStartTime());
            activeBrowseDto.setEndBrowseTime(allDataUserTrendStatisticsDto.getEndTime());
            List<ActiveBrowseVo> activeBrowseVos = activeBrowseApi.listByDto(activeBrowseDto).unpack();
            if (!CollectionUtils.isEmpty(activeBrowseVos)) {
                for (ActiveBrowseVo activeBrowseVo : activeBrowseVos) {
                    LocalDateTime browseTime = activeBrowseVo.getBrowseTime();
                    String mapKey = getMapKey(dateType, browseTime);
//                    String mapKey = LocalDateUtil.toLocalDateToString(browseTime.toLocalDate(), key);
                    List<ActiveBrowseVo> activeBrowseVoList = map.get(mapKey);
                    if (CollectionUtils.isEmpty(activeBrowseVoList)) {
                        activeBrowseVoList = new ArrayList<>();
                    }
                    activeBrowseVoList.add(activeBrowseVo);
                    map.put(mapKey, activeBrowseVoList);
                }
            }


            List<AllDataTrendStatisticsVo> allDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    int browseUserNum = 0;
                    List<ActiveBrowseVo> activeBrowseVoList = map.get(date);
                    if (!CollectionUtils.isEmpty(activeBrowseVoList)) {
                        browseUserNum = activeBrowseVos.stream().map(ActiveBrowseVo::getUserId).collect(Collectors.toSet()).size();
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(browseUserNum));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(allDataUserTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(allDataUserTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    int browseUserNum = 0;
                    List<ActiveBrowseVo> activeBrowseVoList = map.get(date);
                    if (!CollectionUtils.isEmpty(activeBrowseVoList)) {
                        browseUserNum = activeBrowseVos.stream().map(ActiveBrowseVo::getUserId).collect(Collectors.toSet()).size();
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(browseUserNum));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return allDataTrendStatisticsVos;
        } else if (type == 3) {
            // 浏览次数
            Map<String, Integer> map = new HashMap<>();

            ActiveBrowseStatisticsDto activeBrowseStatisticsDto = new ActiveBrowseStatisticsDto();
            activeBrowseStatisticsDto.setBusinessId(businessId);
            activeBrowseStatisticsDto.setStartCreateTime(allDataUserTrendStatisticsDto.getStartTime());
            activeBrowseStatisticsDto.setEndCreateTime(allDataUserTrendStatisticsDto.getEndTime());
            List<ActiveBrowseStatisticsVo> activeBrowseStatisticsVos = activeBrowseStatisticsApi.listByDto(activeBrowseStatisticsDto).unpack();
            if (!CollectionUtils.isEmpty(activeBrowseStatisticsVos)) {
                for (ActiveBrowseStatisticsVo activeBrowseVo : activeBrowseStatisticsVos) {
                    LocalDateTime browseTime = activeBrowseVo.getCreateTime();
                    String mapKey = getMapKey(dateType, browseTime);
                    Integer num = map.get(mapKey);
                    if (num == null) {
                        num = 0;
                    }
                    num += 1;
                    map.put(mapKey, num);
                }
            }

//            ActiveBrowseDto activeBrowseDto = new ActiveBrowseDto();
//            activeBrowseDto.setBusinessId(businessId);
//            activeBrowseDto.setStartBrowseTime(allDataUserTrendStatisticsDto.getStartTime());
//            activeBrowseDto.setEndBrowseTime(allDataUserTrendStatisticsDto.getEndTime());
//            List<ActiveBrowseVo> activeBrowseVos = activeBrowseApi.listByDto(activeBrowseDto).unpack();
//            if (!CollectionUtils.isEmpty(activeBrowseVos)) {
//                for (ActiveBrowseVo activeBrowseVo : activeBrowseVos) {
//                    LocalDateTime browseTime = activeBrowseVo.getBrowseTime();
//                    String mapKey = getMapKey(dateType, browseTime);
////                    String mapKey = LocalDateUtil.toLocalDateToString(browseTime.toLocalDate(), key);
//                    Integer num = map.get(mapKey);
//                    if (num == null) {
//                        num = 0;
//                    }
//                    num += 1;
//                    map.put(mapKey, num);
//                }
//            }


            List<AllDataTrendStatisticsVo> allDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    Integer num = map.get(date);
                    if (num == null) {
                        num = 0;
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(num));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(allDataUserTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(allDataUserTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    Integer num = map.get(date);
                    if (num == null) {
                        num = 0;
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(num));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return allDataTrendStatisticsVos;
        } else if (type == 4) {
            // 分享人数
            Map<String, List<ActiveShareStatisticsVo>> map = new HashMap<>();

            ActiveShareStatisticsDto activeShareStatisticsDto = new ActiveShareStatisticsDto();
            activeShareStatisticsDto.setBusinessId(businessId);
            activeShareStatisticsDto.setStartShareTime(allDataUserTrendStatisticsDto.getStartTime());
            activeShareStatisticsDto.setEndShareTime(allDataUserTrendStatisticsDto.getEndTime());
            List<ActiveShareStatisticsVo> activeShareStatisticsVos = activeShareApi.listByDto(activeShareStatisticsDto).unpack();
            if (!CollectionUtils.isEmpty(activeShareStatisticsVos)) {
                for (ActiveShareStatisticsVo activeShareStatisticsVo : activeShareStatisticsVos) {
                    LocalDateTime browseTime = activeShareStatisticsVo.getShareTime();
                    String mapKey = getMapKey(dateType, browseTime);
//                    String mapKey = LocalDateUtil.toLocalDateToString(browseTime.toLocalDate(), key);
                    List<ActiveShareStatisticsVo> activeShareStatisticsVoList = map.get(mapKey);
                    if (CollectionUtils.isEmpty(activeShareStatisticsVoList)) {
                        activeShareStatisticsVoList = new ArrayList<>();
                    }
                    activeShareStatisticsVoList.add(activeShareStatisticsVo);
                    map.put(mapKey, activeShareStatisticsVoList);
                }
            }


            List<AllDataTrendStatisticsVo> allDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    int shareUserNum = 0;
                    List<ActiveShareStatisticsVo> activeShareStatisticsVoList = map.get(date);
                    if (!CollectionUtils.isEmpty(activeShareStatisticsVoList)) {
                        shareUserNum = activeShareStatisticsVoList.stream().map(ActiveShareStatisticsVo::getUserId).collect(Collectors.toSet()).size();
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(shareUserNum));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(allDataUserTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(allDataUserTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    int shareUserNum = 0;
                    List<ActiveShareStatisticsVo> activeShareStatisticsVoList = map.get(date);
                    if (!CollectionUtils.isEmpty(activeShareStatisticsVoList)) {
                        shareUserNum = activeShareStatisticsVoList.stream().map(ActiveShareStatisticsVo::getUserId).collect(Collectors.toSet()).size();
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(shareUserNum));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return allDataTrendStatisticsVos;
        } else if (type == 5) {
            // 新增订阅人数
            Map<String, List<BusinessSubscribeVo>> map = new HashMap<>();

            BusinessSubscribeDto businessSubscribeDto = new BusinessSubscribeDto();
            businessSubscribeDto.setBusinessId(businessId);
            businessSubscribeDto.setStartTime(allDataUserTrendStatisticsDto.getStartTime());
            businessSubscribeDto.setEndTime(allDataUserTrendStatisticsDto.getEndTime());
            List<BusinessSubscribeVo> businessSubscribeVos = businessSubscribeApi.listByDto(businessSubscribeDto).unpack();
            if (!CollectionUtils.isEmpty(businessSubscribeVos)) {
                for (BusinessSubscribeVo businessSubscribeVo : businessSubscribeVos) {
                    LocalDateTime createTime = businessSubscribeVo.getCreateTime();
                    String mapKey = getMapKey(dateType, createTime);
//                    String mapKey = LocalDateUtil.toLocalDateToString(createTime.toLocalDate(), key);
                    List<BusinessSubscribeVo> businessSubscribeVoList = map.get(mapKey);
                    if (CollectionUtils.isEmpty(businessSubscribeVoList)) {
                        businessSubscribeVoList = new ArrayList<>();
                    }
                    businessSubscribeVoList.add(businessSubscribeVo);
                    map.put(mapKey, businessSubscribeVoList);
                }
            }


            List<AllDataTrendStatisticsVo> allDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    int businessSubscribeNum = 0;
                    List<BusinessSubscribeVo> businessSubscribeVoList = map.get(date);
                    if (!CollectionUtils.isEmpty(businessSubscribeVoList)) {
                        businessSubscribeNum = businessSubscribeVoList.stream().map(BusinessSubscribeVo::getUserId).collect(Collectors.toSet()).size();
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(businessSubscribeNum));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(allDataUserTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(allDataUserTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    int businessSubscribeNum = 0;
                    List<BusinessSubscribeVo> businessSubscribeVoList = map.get(date);
                    if (!CollectionUtils.isEmpty(businessSubscribeVoList)) {
                        businessSubscribeNum = businessSubscribeVoList.stream().map(BusinessSubscribeVo::getUserId).collect(Collectors.toSet()).size();
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(businessSubscribeNum));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return allDataTrendStatisticsVos;
        } else if (type == 6) {
            // 累计订阅人数
            Map<String, List<BusinessSubscribeVo>> map = new HashMap<>();

            BusinessSubscribeDto businessSubscribeDto = new BusinessSubscribeDto();
            businessSubscribeDto.setBusinessId(businessId);
            businessSubscribeDto.setStartTime(allDataUserTrendStatisticsDto.getStartTime());
            businessSubscribeDto.setEndTime(allDataUserTrendStatisticsDto.getEndTime());
            List<BusinessSubscribeVo> businessSubscribeVos = businessSubscribeApi.listByDto(businessSubscribeDto).unpack();
            if (!CollectionUtils.isEmpty(businessSubscribeVos)) {
                for (BusinessSubscribeVo businessSubscribeVo : businessSubscribeVos) {
                    LocalDateTime createTime = businessSubscribeVo.getCreateTime();
                    String mapKey = getMapKey(dateType, createTime);
//                    String mapKey = LocalDateUtil.toLocalDateToString(createTime.toLocalDate(), key);
                    List<BusinessSubscribeVo> businessSubscribeVoList = map.get(mapKey);
                    if (CollectionUtils.isEmpty(businessSubscribeVoList)) {
                        businessSubscribeVoList = new ArrayList<>();
                    }
                    businessSubscribeVoList.add(businessSubscribeVo);
                    map.put(mapKey, businessSubscribeVoList);
                }
            }


            List<AllDataTrendStatisticsVo> allDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    int shareUserNum = 0;
                    List<BusinessSubscribeVo> businessSubscribeVoList = map.get(date);
                    if (!CollectionUtils.isEmpty(businessSubscribeVoList)) {
                        shareUserNum = businessSubscribeVoList.stream().map(BusinessSubscribeVo::getUserId).collect(Collectors.toSet()).size();
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(shareUserNum));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(allDataUserTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(allDataUserTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    int shareUserNum = 0;
                    List<BusinessSubscribeVo> businessSubscribeVoList = map.get(date);
                    if (!CollectionUtils.isEmpty(businessSubscribeVoList)) {
                        shareUserNum = businessSubscribeVoList.stream().map(BusinessSubscribeVo::getUserId).collect(Collectors.toSet()).size();
                    }

                    AllDataTrendStatisticsVo allDataTrendStatisticsVo = new AllDataTrendStatisticsVo();
                    allDataTrendStatisticsVo.setDate(date);
                    allDataTrendStatisticsVo.setValue(String.valueOf(shareUserNum));
                    allDataTrendStatisticsVos.add(allDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return allDataTrendStatisticsVos;
        }

        return null;
    }

    private String getMapKey(Integer dateType, LocalDateTime browseTime) {
        String mapKey;
        if (dateType == 1) {
            int hour = browseTime.getHour();
            mapKey = hour > 9 ? String.valueOf(hour) : "0" + hour;
        } else {
            mapKey = LocalDateUtil.toLocalDateToString(browseTime.toLocalDate(), Constant.YYYY_MM_DD);
        }
        return mapKey;
    }

    public SellDataStatisticsVo getSellData(SellDataStatisticsDto sellDataStatisticsDto) {
        Long businessId = sellDataStatisticsDto.getBusinessId();
        if (businessId == null || businessId <= 0) {
            Long userId = sellDataStatisticsDto.getUserId();
            if (userId == null || userId <= 0) {
                Throw.isBusinessException(ResponseEnums.USER_ACCESS_DENIED);
            }

            BusinessPageVo businessPageVo = businessApi.getByUserId(userId).unpack();
            if (businessPageVo == null) {
                Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
            }

            sellDataStatisticsDto.setBusinessId(businessPageVo.getId());
        }

        // 日期类型：1-今日，2-本周，3-本月，4-自定义
        Integer dateType = sellDataStatisticsDto.getDateType();
        if (dateType == null) {
            Throw.isBusinessException("请选择查询日期类型");
        }

        LocalDate nowLocalDate = LocalDate.now();
        if (dateType == 1) {
            String nowDateStr = LocalDateUtil.toLocalDateToString(nowLocalDate, Constant.YYYY_MM_DD);

            sellDataStatisticsDto.setStartTime(nowDateStr + " 00:00:00");
            sellDataStatisticsDto.setEndTime(nowDateStr + " 23:59:59");
        } else if (dateType == 2) {
            // 本周开始日期
            LocalDate monday = nowLocalDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            // 本周结束日期
            LocalDate sunday = nowLocalDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));

            String mondayStartStr = LocalDateUtil.toLocalDateToString(monday, Constant.YYYY_MM_DD);
            String sundayEndStr = LocalDateUtil.toLocalDateToString(sunday, Constant.YYYY_MM_DD);

            sellDataStatisticsDto.setStartTime(mondayStartStr + " 00:00:00");
            sellDataStatisticsDto.setEndTime(sundayEndStr + " 23:59:59");
        } else if (dateType == 3) {
            // 本月1号
            LocalDate firstDayOfMonth = nowLocalDate.with(TemporalAdjusters.firstDayOfMonth());
            // 本月最后一天
            LocalDate lastDayOfMonth = nowLocalDate.with(TemporalAdjusters.lastDayOfMonth());

            String firstDayOfMonthStartStr = LocalDateUtil.toLocalDateToString(firstDayOfMonth, Constant.YYYY_MM_DD);
            String lastDayOfMonthEndStr = LocalDateUtil.toLocalDateToString(lastDayOfMonth, Constant.YYYY_MM_DD);

            sellDataStatisticsDto.setStartTime(firstDayOfMonthStartStr + " 00:00:00");
            sellDataStatisticsDto.setEndTime(lastDayOfMonthEndStr + " 23:59:59");
        } else if (dateType == 4) {
            String startTime = sellDataStatisticsDto.getStartTime();
            String endTime = sellDataStatisticsDto.getEndTime();
            if (StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)) {
                Throw.isBusinessException("请选择查询日期");
            }

            LocalDate startLocalDate = null;
            try {
                startLocalDate = LocalDateUtil.toLocalDate(startTime, Constant.YYYY_MM_DD);
            } catch (Exception e) {
                Throw.isBusinessException("查询开始日期格式错误");
            }

            LocalDate endLocalDate = null;
            try {
                endLocalDate = LocalDateUtil.toLocalDate(endTime, Constant.YYYY_MM_DD);
            } catch (Exception e) {
                Throw.isBusinessException("查询结束日期格式错误");
            }

            // 自定义日期最长不超过3个月
            long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate);
            if (days > 90) {
                Throw.isBusinessException("自定义日期最长不超过90天");
            }

            sellDataStatisticsDto.setStartTime(startTime + " 00:00:00");
            sellDataStatisticsDto.setEndTime(endTime + " 23:59:59");
        }

        // 自卖订单金额
        BigDecimal totalOrderAmt = BigDecimal.ZERO;
        // 自卖订单数
        int totalOrderNum = 0;
        // 自卖退款金额
        BigDecimal refundOrderAmt = BigDecimal.ZERO;
        // 自卖退款订单数
        int refundOrderNum = 0;

        OrderDto orderDto = new OrderDto();
        orderDto.setBusinessId(businessId);
        orderDto.setAgentId(0L);
        orderDto.setStartOrderTime(sellDataStatisticsDto.getStartTime());
        orderDto.setEndOrderTime(sellDataStatisticsDto.getEndTime());
        List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
        if (!CollectionUtils.isEmpty(orderVos)) {
            for (OrderVo orderVo : orderVos) {
                Integer orderState = orderVo.getOrderState();
                if (OrderStateEnum.WAIT_VERIFY.getValue().equals(orderState) || OrderStateEnum.VERIFYING.getValue().equals(orderState)
                        || OrderStateEnum.FINISH.getValue().equals(orderState) || OrderStateEnum.VERIFIED.getValue().equals(orderState)) {
                    // 实付金额
                    BigDecimal payPrice = orderVo.getPayPrice();
                    // 总订单金额
                    totalOrderAmt = totalOrderAmt.add(payPrice);
                    // 总订单数
                    totalOrderNum += 1;

                } else if (OrderStateEnum.CANCEL.getValue().equals(orderState)) {
                    // 实付金额
                    BigDecimal payPrice = orderVo.getPayPrice();
                    // 总退款金额
                    refundOrderAmt = refundOrderAmt.add(payPrice);

                    refundOrderNum += 1;
                }
            }

        }


//        // 根据日期时间查询订单数据，获取总订单金额（实付金额）
//        OrderDto orderDto = new OrderDto();
//        orderDto.setBusinessId(businessId);
//        orderDto.setAgentId(0L);
//        orderDto.setStartOrderTime(sellDataStatisticsDto.getStartTime());
//        orderDto.setEndOrderTime(sellDataStatisticsDto.getEndTime());
//        orderDto.setOrderStates(Arrays.asList(OrderStateEnum.WAIT_VERIFY.getValue(), OrderStateEnum.VERIFYING.getValue(),
//                OrderStateEnum.FINISH.getValue(), OrderStateEnum.VERIFIED.getValue()));
//        List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
//        if (!CollectionUtils.isEmpty(orderVos)) {
//            for (OrderVo orderVo : orderVos) {
//                // 实付金额
//                BigDecimal payPrice = orderVo.getPayPrice();
//                // 总订单金额
//                totalOrderAmt = totalOrderAmt.add(payPrice);
//            }
//        }
//
//        // 根据日期时间查询订单项数据，获取自卖订单金额，自卖订单数，自卖退款金额，自卖退款订单数
//        OrderItemDto orderItemDto = new OrderItemDto();
//        orderItemDto.setBusinessId(businessId);
//        orderItemDto.setAgentId(0L);
//        orderItemDto.setStartOrderTime(sellDataStatisticsDto.getStartTime());
//        orderItemDto.setEndOrderTime(sellDataStatisticsDto.getEndTime());
//        List<OrderItemVo> orderItemVos = orderItemApi.listByDto(orderItemDto).unpack();
//        if (!CollectionUtils.isEmpty(orderItemVos)) {
//            // 成功下单的订单项列表
//            List<OrderItemVo> successOrderItems = new ArrayList<>();
//            List<OrderItemVo> cancelOrderItems = new ArrayList<>();
//
//            for (OrderItemVo orderItemVo : orderItemVos) {
//                Integer subOrderState = orderItemVo.getSubOrderState();
//                // 商户订单本金(按比例分配的用户支付的订单款)
//                BigDecimal merchantAmt = orderItemVo.getMerchantAmt();
//                BigDecimal distributionOrderAmt = orderItemVo.getDistributionOrderAmt();
//                if (SubOrderStateEnum.WAIT_VERIFY.getValue().equals(subOrderState) || SubOrderStateEnum.VERIFIED.getValue().equals(subOrderState)) {
//                    successOrderItems.add(orderItemVo);
//                } else if (SubOrderStateEnum.CANCEL.getValue().equals(subOrderState) || SubOrderStateEnum.EXPIRED.getValue().equals(subOrderState)) {
//                    // 累加总退款金额
//                    refundOrderAmt = refundOrderAmt.add(distributionOrderAmt);
//                    cancelOrderItems.add(orderItemVo);
//                }
//            }
//
//            if (!CollectionUtils.isEmpty(successOrderItems)) {
//                // 总订单数
//                totalOrderNum = successOrderItems.stream().map(OrderItemVo::getOrderId).collect(Collectors.toSet()).size();
//            }
//
//            if (!CollectionUtils.isEmpty(cancelOrderItems)) {
//                // 退款订单数
//                refundOrderNum = cancelOrderItems.stream().map(OrderItemVo::getOrderId).collect(Collectors.toSet()).size();
//            }
//        }

        SellDataStatisticsVo sellDataStatisticsVo = new SellDataStatisticsVo();
        sellDataStatisticsVo.setTotalOrderAmt(totalOrderAmt);
        sellDataStatisticsVo.setTotalOrderNum(totalOrderNum);
        sellDataStatisticsVo.setRefundOrderAmt(refundOrderAmt);
        sellDataStatisticsVo.setRefundOrderNum(refundOrderNum);

        return sellDataStatisticsVo;
    }

    public List<SellDataTrendStatisticsVo> getSellDataTrend(SellDataTrendStatisticsDto sellDataTrendStatisticsDto) {
        Long businessId = sellDataTrendStatisticsDto.getBusinessId();
        if (businessId == null || businessId <= 0) {
            Long userId = sellDataTrendStatisticsDto.getUserId();
            if (userId == null || userId <= 0) {
                Throw.isBusinessException(ResponseEnums.USER_ACCESS_DENIED);
            }

            BusinessPageVo businessPageVo = businessApi.getByUserId(userId).unpack();
            if (businessPageVo == null) {
                Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
            }

            sellDataTrendStatisticsDto.setBusinessId(businessPageVo.getId());
        }

        // 类型：1-总订单金额，2-总订单数，3-总下单人数，4-总补贴金额，5-浏览人数，6-浏览次数，7-下单新用户，8-退款金额
        Integer type = sellDataTrendStatisticsDto.getType();
        if (type == null) {
            Throw.isBusinessException("请选择查询类型");
        }

        // 日期类型：1-今日，2-本周，3-本月，4-自定义
        Integer dateType = sellDataTrendStatisticsDto.getDateType();
        if (dateType == null) {
            Throw.isBusinessException("请选择查询日期类型");
        }

        LocalDate nowLocalDate = LocalDate.now();
        if (dateType == 1) {
            String nowDateStr = LocalDateUtil.toLocalDateToString(nowLocalDate, Constant.YYYY_MM_DD);

            sellDataTrendStatisticsDto.setStartTime(nowDateStr + " 00:00:00");
            sellDataTrendStatisticsDto.setEndTime(nowDateStr + " 23:59:59");
        } else if (dateType == 2) {
            // 本周开始日期
            LocalDate monday = nowLocalDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            // 本周结束日期
            LocalDate sunday = nowLocalDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));

            String mondayStartStr = LocalDateUtil.toLocalDateToString(monday, Constant.YYYY_MM_DD);
            String sundayEndStr = LocalDateUtil.toLocalDateToString(sunday, Constant.YYYY_MM_DD);

            sellDataTrendStatisticsDto.setStartTime(mondayStartStr + " 00:00:00");
            sellDataTrendStatisticsDto.setEndTime(sundayEndStr + " 23:59:59");
        } else if (dateType == 3) {
            // 本月1号
            LocalDate firstDayOfMonth = nowLocalDate.with(TemporalAdjusters.firstDayOfMonth());
            // 本月最后一天
            LocalDate lastDayOfMonth = nowLocalDate.with(TemporalAdjusters.lastDayOfMonth());

            String firstDayOfMonthStartStr = LocalDateUtil.toLocalDateToString(firstDayOfMonth, Constant.YYYY_MM_DD);
            String lastDayOfMonthEndStr = LocalDateUtil.toLocalDateToString(lastDayOfMonth, Constant.YYYY_MM_DD);

            sellDataTrendStatisticsDto.setStartTime(firstDayOfMonthStartStr + " 00:00:00");
            sellDataTrendStatisticsDto.setEndTime(lastDayOfMonthEndStr + " 23:59:59");
        } else if (dateType == 4) {
            String startTime = sellDataTrendStatisticsDto.getStartTime();
            String endTime = sellDataTrendStatisticsDto.getEndTime();
            if (StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)) {
                Throw.isBusinessException("请选择查询日期");
            }

            LocalDate startLocalDate = null;
            try {
                startLocalDate = LocalDateUtil.toLocalDate(startTime, Constant.YYYY_MM_DD);
            } catch (Exception e) {
                Throw.isBusinessException("查询开始日期格式错误");
            }

            LocalDate endLocalDate = null;
            try {
                endLocalDate = LocalDateUtil.toLocalDate(endTime, Constant.YYYY_MM_DD);
            } catch (Exception e) {
                Throw.isBusinessException("查询结束日期格式错误");
            }

            // 自定义日期最长不超过3个月
            long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate);
            if (days > 90) {
                Throw.isBusinessException("自定义日期最长不超过90天");
            }

            sellDataTrendStatisticsDto.setStartTime(startTime + " 00:00:00");
            sellDataTrendStatisticsDto.setEndTime(endTime + " 23:59:59");
        }

        //  今日 24小时  3或4小时一条
        //  本周  7天  按天
        //  其他日期按天（只能选到天））  最大不超过3个月
        // 根据日期类型判断是根据小时分组还是日期
//        String key = dateType == 1 ? "HH" : "yyyy-MM-dd";

        // 类型：1-自卖订单金额，2-自卖订单数，3-自卖退款金额，4-自卖退款订单数
        if (type == 1) {
            // 自卖订单金额
            Map<String, BigDecimal> map = new HashMap<>();

            // 根据日期时间查询订单数据，获取总订单金额（实付金额）
            OrderDto orderDto = new OrderDto();
            orderDto.setBusinessId(businessId);
            orderDto.setAgentId(0L);
            orderDto.setStartOrderTime(sellDataTrendStatisticsDto.getStartTime());
            orderDto.setEndOrderTime(sellDataTrendStatisticsDto.getEndTime());
            orderDto.setOrderStates(Arrays.asList(OrderStateEnum.WAIT_VERIFY.getValue(), OrderStateEnum.VERIFYING.getValue(),
                    OrderStateEnum.FINISH.getValue(), OrderStateEnum.VERIFIED.getValue()));
            List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
            if (!CollectionUtils.isEmpty(orderVos)) {
                for (OrderVo orderVo : orderVos) {
                    // 实付金额
                    BigDecimal payPrice = orderVo.getPayPrice();
                    // 下单时间
                    LocalDateTime orderTime = orderVo.getOrderTime();
                    String mapKey = getMapKey(dateType, orderTime);
//                    String mapKey = LocalDateUtil.toLocalDateToString(orderTime.toLocalDate(), key);
                    BigDecimal amt = map.get(mapKey);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }
                    amt = amt.add(payPrice);
                    map.put(mapKey, amt);
                }
            }

            List<SellDataTrendStatisticsVo> sellDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    BigDecimal amt = map.get(date);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }

                    SellDataTrendStatisticsVo sellDataTrendStatisticsVo = new SellDataTrendStatisticsVo();
                    sellDataTrendStatisticsVo.setDate(date);
                    sellDataTrendStatisticsVo.setValue(amt.toString());
                    sellDataTrendStatisticsVos.add(sellDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(sellDataTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(sellDataTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    BigDecimal amt = map.get(date);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }

                    SellDataTrendStatisticsVo sellDataTrendStatisticsVo = new SellDataTrendStatisticsVo();
                    sellDataTrendStatisticsVo.setDate(date);
                    sellDataTrendStatisticsVo.setValue(amt.toString());
                    sellDataTrendStatisticsVos.add(sellDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return sellDataTrendStatisticsVos;
        } else if (type == 2) {
            // 自卖订单数
            Map<String, Integer> map = new HashMap<>();

            OrderDto orderDto = new OrderDto();
            orderDto.setBusinessId(businessId);
            orderDto.setNoAgentId(0L);
            orderDto.setStartOrderTime(sellDataTrendStatisticsDto.getStartTime());
            orderDto.setEndOrderTime(sellDataTrendStatisticsDto.getEndTime());
            orderDto.setOrderStates(Arrays.asList(OrderStateEnum.FINISH.getValue(),
                    OrderStateEnum.WAIT_VERIFY.getValue(),
                    OrderStateEnum.VERIFYING.getValue(),
                    OrderStateEnum.VERIFIED.getValue()));
            List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
            if (!CollectionUtils.isEmpty(orderVos)) {
                for (OrderVo orderVo : orderVos) {
                    // 下单时间
                    LocalDateTime orderTime = orderVo.getOrderTime();
                    String mapKey = getMapKey(dateType, orderTime);
                    Integer count = map.get(mapKey);
                    if (count == null) {
                        count = 0;
                    }
                    count += 1;
                    map.put(mapKey, count);
                }
            }

//            OrderItemDto orderItemDto = new OrderItemDto();
//            orderItemDto.setBusinessId(businessId);
//            orderItemDto.setAgentId(0L);
//            orderItemDto.setStartOrderTime(sellDataTrendStatisticsDto.getStartTime());
//            orderItemDto.setEndOrderTime(sellDataTrendStatisticsDto.getEndTime());
//            List<OrderItemVo> orderItemVos = orderItemApi.listByDto(orderItemDto).unpack();
//            if (!CollectionUtils.isEmpty(orderItemVos)) {
//                // 成功下单的订单项列表
//                List<OrderItemVo> orderItemVoList = orderItemVos.stream().filter(orderItemVo ->
//                        (SubOrderStateEnum.WAIT_VERIFY.getValue().equals(orderItemVo.getSubOrderState())
//                                || SubOrderStateEnum.VERIFIED.getValue().equals(orderItemVo.getSubOrderState()))).collect(Collectors.toList());
//                if (!CollectionUtils.isEmpty(orderItemVoList)) {
//                    for (OrderItemVo orderItemVo : orderItemVoList) {
//                        // 下单时间
//                        LocalDateTime orderTime = orderItemVo.getOrderTime();
//                        String mapKey = getMapKey(dateType, orderTime);
//                        List<OrderItemVo> orderItems = map.get(mapKey);
//                        if (CollectionUtils.isEmpty(orderItems)) {
//                            orderItems = new ArrayList<>();
//                        }
//                        orderItems.add(orderItemVo);
//                        map.put(mapKey, orderItems);
//                    }
//                }
//            }


            List<SellDataTrendStatisticsVo> sellDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    int orderNum = 0;
                    Integer count = map.get(date);
                    if (count != null) {
                        orderNum = count;
                    }

                    SellDataTrendStatisticsVo sellDataTrendStatisticsVo = new SellDataTrendStatisticsVo();
                    sellDataTrendStatisticsVo.setDate(date);
                    sellDataTrendStatisticsVo.setValue(String.valueOf(orderNum));
                    sellDataTrendStatisticsVos.add(sellDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(sellDataTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(sellDataTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    int orderNum = 0;
                    Integer count = map.get(date);
                    if (count != null) {
                        orderNum = count;
                    }

                    SellDataTrendStatisticsVo sellDataTrendStatisticsVo = new SellDataTrendStatisticsVo();
                    sellDataTrendStatisticsVo.setDate(date);
                    sellDataTrendStatisticsVo.setValue(String.valueOf(orderNum));
                    sellDataTrendStatisticsVos.add(sellDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return sellDataTrendStatisticsVos;
        } else if (type == 3) {
            // 自卖退款金额
            Map<String, BigDecimal> map = new HashMap<>();

            OrderDto orderDto = new OrderDto();
            orderDto.setBusinessId(businessId);
            orderDto.setNoAgentId(0L);
            orderDto.setStartOrderTime(sellDataTrendStatisticsDto.getStartTime());
            orderDto.setEndOrderTime(sellDataTrendStatisticsDto.getEndTime());
            orderDto.setOrderStates(Collections.singletonList(OrderStateEnum.CANCEL.getValue()));
            List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
            if (!CollectionUtils.isEmpty(orderVos)) {
                for (OrderVo orderVo : orderVos) {
                    // 下单时间
                    LocalDateTime orderTime = orderVo.getOrderTime();
                    String mapKey = getMapKey(dateType, orderTime);
                    BigDecimal amt = map.get(mapKey);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }
                    amt = amt.add(orderVo.getPayPrice());
                    map.put(mapKey, amt);
                }
            }

//            OrderItemDto orderItemDto = new OrderItemDto();
//            orderItemDto.setBusinessId(businessId);
//            orderItemDto.setAgentId(0L);
//            orderItemDto.setStartOrderTime(sellDataTrendStatisticsDto.getStartTime());
//            orderItemDto.setEndOrderTime(sellDataTrendStatisticsDto.getEndTime());
//            List<OrderItemVo> orderItemVos = orderItemApi.listByDto(orderItemDto).unpack();
//            if (!CollectionUtils.isEmpty(orderItemVos)) {
//                // 取消下单的订单项列表
//                List<OrderItemVo> orderItemVoList = orderItemVos.stream().filter(orderItemVo ->
//                        (SubOrderStateEnum.CANCEL.getValue().equals(orderItemVo.getSubOrderState())
//                                || SubOrderStateEnum.EXPIRED.getValue().equals(orderItemVo.getSubOrderState()))).collect(Collectors.toList());
//                if (!CollectionUtils.isEmpty(orderItemVoList)) {
//                    for (OrderItemVo orderItemVo : orderItemVoList) {
//                        // 商户订单本金(按比例分配的用户支付的订单款)
//                        BigDecimal merchantAmt = orderItemVo.getMerchantAmt();
//                        BigDecimal distributionOrderAmt = orderItemVo.getDistributionOrderAmt();
//                        // 下单时间
//                        LocalDateTime orderTime = orderItemVo.getOrderTime();
//                        String mapKey = getMapKey(dateType, orderTime);
////                        String mapKey = LocalDateUtil.toLocalDateToString(orderTime.toLocalDate(), key);
//                        BigDecimal amt = map.get(mapKey);
//                        if (amt == null) {
//                            amt = BigDecimal.ZERO;
//                        }
//                        amt = amt.add(distributionOrderAmt);
//                        map.put(mapKey, amt);
//                    }
//                }
//            }


            List<SellDataTrendStatisticsVo> sellDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    BigDecimal amt = map.get(date);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }

                    SellDataTrendStatisticsVo sellDataTrendStatisticsVo = new SellDataTrendStatisticsVo();
                    sellDataTrendStatisticsVo.setDate(date);
                    sellDataTrendStatisticsVo.setValue(amt.toString());
                    sellDataTrendStatisticsVos.add(sellDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(sellDataTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(sellDataTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    BigDecimal amt = map.get(date);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }

                    SellDataTrendStatisticsVo sellDataTrendStatisticsVo = new SellDataTrendStatisticsVo();
                    sellDataTrendStatisticsVo.setDate(date);
                    sellDataTrendStatisticsVo.setValue(amt.toString());
                    sellDataTrendStatisticsVos.add(sellDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return sellDataTrendStatisticsVos;
        } else if (type == 4) {
            // 自卖退款订单数
            Map<String, Integer> map = new HashMap<>();

            // 根据日期时间查询商家所有的订单数据,获取分销订单金额,分销订单数,分销退款金额,分销退款订单数,已结算佣金
            OrderDto orderDto = new OrderDto();
            orderDto.setBusinessId(businessId);
            orderDto.setNoAgentId(0L);
            orderDto.setStartOrderTime(sellDataTrendStatisticsDto.getStartTime());
            orderDto.setEndOrderTime(sellDataTrendStatisticsDto.getEndTime());
            orderDto.setOrderStates(Collections.singletonList(OrderStateEnum.CANCEL.getValue()));
            List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
            if (!CollectionUtils.isEmpty(orderVos)) {
                for (OrderVo orderVo : orderVos) {
                    // 下单时间
                    LocalDateTime orderTime = orderVo.getOrderTime();
                    String mapKey = getMapKey(dateType, orderTime);
                    Integer count = map.get(mapKey);
                    if (count == null) {
                        count = 0;
                    }
                    count += 1;
                    map.put(mapKey, count);
                }
            }


//            OrderItemDto orderItemDto = new OrderItemDto();
//            orderItemDto.setBusinessId(businessId);
//            orderItemDto.setAgentId(0L);
//            orderItemDto.setStartOrderTime(sellDataTrendStatisticsDto.getStartTime());
//            orderItemDto.setEndOrderTime(sellDataTrendStatisticsDto.getEndTime());
//            List<OrderItemVo> orderItemVos = orderItemApi.listByDto(orderItemDto).unpack();
//            if (!CollectionUtils.isEmpty(orderItemVos)) {
//                // 取消下单的订单项列表
//                List<OrderItemVo> orderItemVoList = orderItemVos.stream().filter(orderItemVo ->
//                        (SubOrderStateEnum.CANCEL.getValue().equals(orderItemVo.getSubOrderState())
//                                || SubOrderStateEnum.EXPIRED.getValue().equals(orderItemVo.getSubOrderState()))).collect(Collectors.toList());
//                if (!CollectionUtils.isEmpty(orderItemVoList)) {
//                    for (OrderItemVo orderItemVo : orderItemVoList) {
//                        // 下单时间
//                        LocalDateTime orderTime = orderItemVo.getOrderTime();
//                        String mapKey = getMapKey(dateType, orderTime);
//                        List<OrderItemVo> orderItems = map.get(mapKey);
//                        if (CollectionUtils.isEmpty(orderItems)) {
//                            orderItems = new ArrayList<>();
//                        }
//                        orderItems.add(orderItemVo);
//                        map.put(mapKey, orderItems);
//                    }
//                }
//            }


            List<SellDataTrendStatisticsVo> sellDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    int orderNum = 0;
                    Integer count = map.get(date);
                    if (count != null) {
                        orderNum = count;
                    }

                    SellDataTrendStatisticsVo sellDataTrendStatisticsVo = new SellDataTrendStatisticsVo();
                    sellDataTrendStatisticsVo.setDate(date);
                    sellDataTrendStatisticsVo.setValue(String.valueOf(orderNum));
                    sellDataTrendStatisticsVos.add(sellDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(sellDataTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(sellDataTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    int orderNum = 0;
                    Integer count = map.get(date);
                    if (count != null) {
                        orderNum = count;
                    }

                    SellDataTrendStatisticsVo sellDataTrendStatisticsVo = new SellDataTrendStatisticsVo();
                    sellDataTrendStatisticsVo.setDate(date);
                    sellDataTrendStatisticsVo.setValue(String.valueOf(orderNum));
                    sellDataTrendStatisticsVos.add(sellDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return sellDataTrendStatisticsVos;
        }

        return null;
    }

    public AgentDataStatisticsVo getAgentData(AgentDataStatisticsDto agentDataStatisticsDto) {
        Long userId = agentDataStatisticsDto.getUserId();
        if (userId == null || userId <= 0) {
            Throw.isBusinessException(ResponseEnums.USER_ACCESS_DENIED);
        }

        Long businessId = agentDataStatisticsDto.getBusinessId();
        if (businessId == null || businessId <= 0) {
            BusinessPageVo businessPageVo = businessApi.getByUserId(userId).unpack();
            if (businessPageVo != null) {
                businessId = businessPageVo.getId();
                agentDataStatisticsDto.setBusinessId(businessId);
            }
        }

        // 日期类型：1-今日，2-本周，3-本月，4-自定义
        Integer dateType = agentDataStatisticsDto.getDateType();
        if (dateType == null) {
            Throw.isBusinessException("请选择查询日期类型");
        }

        LocalDate nowLocalDate = LocalDate.now();
        if (dateType == 1) {
            String nowDateStr = LocalDateUtil.toLocalDateToString(nowLocalDate, Constant.YYYY_MM_DD);

            agentDataStatisticsDto.setStartTime(nowDateStr + " 00:00:00");
            agentDataStatisticsDto.setEndTime(nowDateStr + " 23:59:59");
        } else if (dateType == 2) {
            // 本周开始日期
            LocalDate monday = nowLocalDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            // 本周结束日期
            LocalDate sunday = nowLocalDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));

            String mondayStartStr = LocalDateUtil.toLocalDateToString(monday, Constant.YYYY_MM_DD);
            String sundayEndStr = LocalDateUtil.toLocalDateToString(sunday, Constant.YYYY_MM_DD);

            agentDataStatisticsDto.setStartTime(mondayStartStr + " 00:00:00");
            agentDataStatisticsDto.setEndTime(sundayEndStr + " 23:59:59");
        } else if (dateType == 3) {
            // 本月1号
            LocalDate firstDayOfMonth = nowLocalDate.with(TemporalAdjusters.firstDayOfMonth());
            // 本月最后一天
            LocalDate lastDayOfMonth = nowLocalDate.with(TemporalAdjusters.lastDayOfMonth());

            String firstDayOfMonthStartStr = LocalDateUtil.toLocalDateToString(firstDayOfMonth, Constant.YYYY_MM_DD);
            String lastDayOfMonthEndStr = LocalDateUtil.toLocalDateToString(lastDayOfMonth, Constant.YYYY_MM_DD);

            agentDataStatisticsDto.setStartTime(firstDayOfMonthStartStr + " 00:00:00");
            agentDataStatisticsDto.setEndTime(lastDayOfMonthEndStr + " 23:59:59");
        } else if (dateType == 4) {
            String startTime = agentDataStatisticsDto.getStartTime();
            String endTime = agentDataStatisticsDto.getEndTime();
            if (StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)) {
                Throw.isBusinessException("请选择查询日期");
            }

            LocalDate startLocalDate = null;
            try {
                startLocalDate = LocalDateUtil.toLocalDate(startTime, Constant.YYYY_MM_DD);
            } catch (Exception e) {
                Throw.isBusinessException("查询开始日期格式错误");
            }

            LocalDate endLocalDate = null;
            try {
                endLocalDate = LocalDateUtil.toLocalDate(endTime, Constant.YYYY_MM_DD);
            } catch (Exception e) {
                Throw.isBusinessException("查询结束日期格式错误");
            }

            // 自定义日期最长不超过3个月
            long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate);
            if (days > 90) {
                Throw.isBusinessException("自定义日期最长不超过90天");
            }

            agentDataStatisticsDto.setStartTime(startTime + " 00:00:00");
            agentDataStatisticsDto.setEndTime(endTime + " 23:59:59");
        }

        // 分销订单金额
        BigDecimal totalOrderAmt = BigDecimal.ZERO;
        // 分销订单数
        int totalOrderNum = 0;
        // 分销退款金额
        BigDecimal refundOrderAmt = BigDecimal.ZERO;
        // 分销退款订单数
        int refundOrderNum = 0;
        // 新增经纪人数(商家显示)
        int newAgentUserNum = 0;
        // 已结算佣金(商家显示)
        BigDecimal totalCommissionAmt = BigDecimal.ZERO;

        // 根据日期时间查询商家所有的订单数据,获取分销订单金额,分销订单数,分销退款金额,分销退款订单数,已结算佣金
        OrderDto orderDto = new OrderDto();
        orderDto.setBusinessId(businessId);
        orderDto.setNoAgentId(0L);
        orderDto.setStartOrderTime(agentDataStatisticsDto.getStartTime());
        orderDto.setEndOrderTime(agentDataStatisticsDto.getEndTime());
        List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
        if (!CollectionUtils.isEmpty(orderVos)) {
            List<Long> orderIds = orderVos.stream().map(OrderVo::getId).collect(Collectors.toList());

            OrderItemDto orderItemDto = new OrderItemDto();
            orderItemDto.setOrderIds(orderIds);
            orderItemDto.setSubOrderStates(Collections.singletonList(SubOrderStateEnum.VERIFIED.getValue()));
            List<OrderItemVo> orderItemVos = orderItemApi.listByDto(orderItemDto).unpack();
            Map<Long, List<OrderItemVo>> orderItemMap = new HashMap<>();
            if (!CollectionUtils.isEmpty(orderItemVos)) {
                orderItemMap = orderItemVos.stream().collect(Collectors.groupingBy(OrderItemVo::getOrderId));
            }

            for (OrderVo orderVo : orderVos) {
                Integer orderState = orderVo.getOrderState();
                if (OrderStateEnum.WAIT_VERIFY.getValue().equals(orderState) || OrderStateEnum.VERIFYING.getValue().equals(orderState)
                        || OrderStateEnum.FINISH.getValue().equals(orderState) || OrderStateEnum.VERIFIED.getValue().equals(orderState)) {
                    // 分销订单金额
                    totalOrderAmt = totalOrderAmt.add(orderVo.getPayPrice());
                    // 分销订单数
                    totalOrderNum += 1;

                    if (OrderStateEnum.FINISH.getValue().equals(orderState)) {
                        List<OrderItemVo> orderItemVoList = orderItemMap.get(orderVo.getId());
                        if (!CollectionUtils.isEmpty(orderItemVoList)) {
                            for (OrderItemVo orderItemVo : orderItemVoList) {
                                totalCommissionAmt = totalCommissionAmt.add(orderItemVo.getCommissionAmt());
                            }
                        }
                    }
                } else if (OrderStateEnum.CANCEL.getValue().equals(orderState)) {
                    // 分销退款金额
                    refundOrderAmt = refundOrderAmt.add(orderVo.getPayPrice());
                    // 分销退款订单数
                    refundOrderNum += 1;
                }
            }
        }


        // 根据日期时间查询订单数据，获取总订单金额（实付金额）
//        OrderDto orderDto = new OrderDto();
//        if (businessId != null) {
//            orderDto.setBusinessId(businessId);
//            orderDto.setNoAgentId(0L);
//        } else {
//            orderDto.setAgentId(userId);
//        }
//        orderDto.setStartOrderTime(agentDataStatisticsDto.getStartTime());
//        orderDto.setEndOrderTime(agentDataStatisticsDto.getEndTime());
//        orderDto.setOrderStates(Arrays.asList(OrderStateEnum.WAIT_VERIFY.getValue(), OrderStateEnum.VERIFYING.getValue(),
//                OrderStateEnum.FINISH.getValue(), OrderStateEnum.VERIFIED.getValue()));
//        List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
//        if (!CollectionUtils.isEmpty(orderVos)) {
//            for (OrderVo orderVo : orderVos) {
//                // 实付金额
//                BigDecimal payPrice = orderVo.getPayPrice();
//                // 总订单金额
//                totalOrderAmt = totalOrderAmt.add(payPrice);
//            }
//        }
//
//        // 根据日期时间查询订单项数据，获取总订单金额，总订单数，总下单人数，已结算佣金，总退款金额
//        OrderItemDto orderItemDto = new OrderItemDto();
//        if (businessId != null) {
//            orderItemDto.setBusinessId(businessId);
//            orderItemDto.setNoAgentId(0L);
//        } else {
//            orderItemDto.setAgentId(userId);
//        }
//        orderItemDto.setStartOrderTime(agentDataStatisticsDto.getStartTime());
//        orderItemDto.setEndOrderTime(agentDataStatisticsDto.getEndTime());
//        List<OrderItemVo> orderItemVos = orderItemApi.listByDto(orderItemDto).unpack();
//        if (!CollectionUtils.isEmpty(orderItemVos)) {
//            // 成功下单的订单项列表
//            List<OrderItemVo> successOrderItems = new ArrayList<>();
//            // 退款的订单项列表
//            List<OrderItemVo> cancelOrderItems = new ArrayList<>();
//
//            for (OrderItemVo orderItemVo : orderItemVos) {
//                Integer settleState = orderItemVo.getSettleState();
//                Integer subOrderState = orderItemVo.getSubOrderState();
//                // 商户订单本金(按比例分配的用户支付的订单款)
//                BigDecimal merchantAmt = orderItemVo.getMerchantAmt();
//                BigDecimal distributionOrderAmt = orderItemVo.getDistributionOrderAmt();
//                // 佣金
//                BigDecimal commissionAmt = orderItemVo.getCommissionAmt();
//
//                if (SubOrderStateEnum.WAIT_VERIFY.getValue().equals(subOrderState) || SubOrderStateEnum.VERIFIED.getValue().equals(subOrderState)) {
//                    successOrderItems.add(orderItemVo);
//                } else if (SubOrderStateEnum.CANCEL.getValue().equals(subOrderState) || SubOrderStateEnum.EXPIRED.getValue().equals(subOrderState)) {
//                    // 累加总退款金额
//                    refundOrderAmt = refundOrderAmt.add(distributionOrderAmt);
//                    cancelOrderItems.add(orderItemVo);
//                }
//
//                if (SettleStateEnum.YES.getValue().equals(settleState)) {
//                    totalCommissionAmt = totalCommissionAmt.add(commissionAmt);
//                }
//            }
//
//            if (!CollectionUtils.isEmpty(successOrderItems)) {
//                // 总订单数
//                totalOrderNum = successOrderItems.stream().map(OrderItemVo::getOrderId).collect(Collectors.toSet()).size();
//            }
//
//            if (!CollectionUtils.isEmpty(cancelOrderItems)) {
//                // 退款订单数
//                refundOrderNum = cancelOrderItems.stream().map(OrderItemVo::getOrderId).collect(Collectors.toSet()).size();
//            }
//        }

        // 查询新增经纪人数(商家显示)
        if (businessId != null && businessId > 0) {
            BusinessAgentDto businessAgentDto = new BusinessAgentDto();
            businessAgentDto.setBusinessId(businessId);
            businessAgentDto.setStartTime(agentDataStatisticsDto.getStartTime());
            businessAgentDto.setEndTime(agentDataStatisticsDto.getEndTime());
            List<BusinessAgentVo> businessAgentVos = businessAgentApi.listByDto(businessAgentDto).unpack();
            if (!CollectionUtils.isEmpty(businessAgentVos)) {
                newAgentUserNum = businessAgentVos.size();
            }
        }

        AgentDataStatisticsVo agentDataStatisticsVo = new AgentDataStatisticsVo();
        agentDataStatisticsVo.setTotalOrderAmt(totalOrderAmt);
        agentDataStatisticsVo.setTotalOrderNum(totalOrderNum);
        agentDataStatisticsVo.setRefundOrderAmt(refundOrderAmt);
        agentDataStatisticsVo.setRefundOrderNum(refundOrderNum);
        agentDataStatisticsVo.setNewAgentUserNum(newAgentUserNum);
        agentDataStatisticsVo.setCommissionAmt((businessId != null && businessId > 0) ? totalCommissionAmt : BigDecimal.ZERO);

        return agentDataStatisticsVo;
    }

    public List<AgentDataTrendStatisticsVo> getAgentDataTrend(AgentDataTrendStatisticsDto agentDataTrendStatisticsDto) {
        Long userId = agentDataTrendStatisticsDto.getUserId();
        if (userId == null || userId <= 0) {
            Throw.isBusinessException(ResponseEnums.USER_ACCESS_DENIED);
        }

        Long businessId = agentDataTrendStatisticsDto.getBusinessId();
        if (businessId == null || businessId <= 0) {
            BusinessPageVo businessPageVo = businessApi.getByUserId(userId).unpack();
            if (businessPageVo != null) {
                businessId = businessPageVo.getId();
                agentDataTrendStatisticsDto.setBusinessId(businessId);
            }
        }

        // 类型：1-分销订单金额，2-分销订单数，3-分销退款金额，4-分销退款订单数，5-新增经纪人数，6-已结算佣金
        Integer type = agentDataTrendStatisticsDto.getType();
        if (type == null) {
            Throw.isBusinessException("请选择查询类型");
        }

        // 日期类型：1-今日，2-本周，3-本月，4-自定义
        Integer dateType = agentDataTrendStatisticsDto.getDateType();
        if (dateType == null) {
            Throw.isBusinessException("请选择查询日期类型");
        }

        LocalDate nowLocalDate = LocalDate.now();
        if (dateType == 1) {
            String nowDateStr = LocalDateUtil.toLocalDateToString(nowLocalDate, Constant.YYYY_MM_DD);

            agentDataTrendStatisticsDto.setStartTime(nowDateStr + " 00:00:00");
            agentDataTrendStatisticsDto.setEndTime(nowDateStr + " 23:59:59");
        } else if (dateType == 2) {
            // 本周开始日期
            LocalDate monday = nowLocalDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            // 本周结束日期
            LocalDate sunday = nowLocalDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));

            String mondayStartStr = LocalDateUtil.toLocalDateToString(monday, Constant.YYYY_MM_DD);
            String sundayEndStr = LocalDateUtil.toLocalDateToString(sunday, Constant.YYYY_MM_DD);

            agentDataTrendStatisticsDto.setStartTime(mondayStartStr + " 00:00:00");
            agentDataTrendStatisticsDto.setEndTime(sundayEndStr + " 23:59:59");
        } else if (dateType == 3) {
            // 本月1号
            LocalDate firstDayOfMonth = nowLocalDate.with(TemporalAdjusters.firstDayOfMonth());
            // 本月最后一天
            LocalDate lastDayOfMonth = nowLocalDate.with(TemporalAdjusters.lastDayOfMonth());

            String firstDayOfMonthStartStr = LocalDateUtil.toLocalDateToString(firstDayOfMonth, Constant.YYYY_MM_DD);
            String lastDayOfMonthEndStr = LocalDateUtil.toLocalDateToString(lastDayOfMonth, Constant.YYYY_MM_DD);

            agentDataTrendStatisticsDto.setStartTime(firstDayOfMonthStartStr + " 00:00:00");
            agentDataTrendStatisticsDto.setEndTime(lastDayOfMonthEndStr + " 23:59:59");
        } else if (dateType == 4) {
            String startTime = agentDataTrendStatisticsDto.getStartTime();
            String endTime = agentDataTrendStatisticsDto.getEndTime();
            if (StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)) {
                Throw.isBusinessException("请选择查询日期");
            }

            LocalDate startLocalDate = null;
            try {
                startLocalDate = LocalDateUtil.toLocalDate(startTime, Constant.YYYY_MM_DD);
            } catch (Exception e) {
                Throw.isBusinessException("查询开始日期格式错误");
            }

            LocalDate endLocalDate = null;
            try {
                endLocalDate = LocalDateUtil.toLocalDate(endTime, Constant.YYYY_MM_DD);
            } catch (Exception e) {
                Throw.isBusinessException("查询结束日期格式错误");
            }

            // 自定义日期最长不超过3个月
            long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate);
            if (days > 90) {
                Throw.isBusinessException("自定义日期最长不超过90天");
            }

            agentDataTrendStatisticsDto.setStartTime(startTime + " 00:00:00");
            agentDataTrendStatisticsDto.setEndTime(endTime + " 23:59:59");
        }

        //  今日 24小时  3或4小时一条
        //  本周  7天  按天
        //  其他日期按天（只能选到天））  最大不超过3个月
        // 根据日期类型判断是根据小时分组还是日期
//        String key = dateType == 1 ? "HH" : Constant.YYYY_MM_DD;

        // 类型：1-分销订单金额，2-分销订单数，3-分销退款金额，4-分销退款订单数，5-新增经纪人数，6-已结算佣金
        if (type == 1) {
            // 分销订单金额
            Map<String, BigDecimal> map = new HashMap<>();

            // 根据日期时间查询订单数据，获取总订单金额（实付金额）
            OrderDto orderDto = new OrderDto();
            if (businessId != null) {
                orderDto.setBusinessId(businessId);
                orderDto.setNoAgentId(0L);
            } else {
                orderDto.setAgentId(userId);
            }
            orderDto.setStartOrderTime(agentDataTrendStatisticsDto.getStartTime());
            orderDto.setEndOrderTime(agentDataTrendStatisticsDto.getEndTime());
            orderDto.setOrderStates(Arrays.asList(OrderStateEnum.WAIT_VERIFY.getValue(), OrderStateEnum.VERIFYING.getValue(),
                    OrderStateEnum.FINISH.getValue(), OrderStateEnum.VERIFIED.getValue()));
            List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
            if (!CollectionUtils.isEmpty(orderVos)) {
                for (OrderVo orderVo : orderVos) {
                    // 实付金额
                    BigDecimal payPrice = orderVo.getPayPrice();
                    // 下单时间
                    LocalDateTime orderTime = orderVo.getOrderTime();
                    String mapKey = getMapKey(dateType, orderTime);
//                    String mapKey = LocalDateUtil.toLocalDateToString(orderTime.toLocalDate(), key);
                    BigDecimal amt = map.get(mapKey);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }
                    amt = amt.add(payPrice);
                    map.put(mapKey, amt);
                }
            }

            List<AgentDataTrendStatisticsVo> agentDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    BigDecimal amt = map.get(date);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }

                    AgentDataTrendStatisticsVo agentDataTrendStatisticsVo = new AgentDataTrendStatisticsVo();
                    agentDataTrendStatisticsVo.setDate(date);
                    agentDataTrendStatisticsVo.setValue(amt.toString());
                    agentDataTrendStatisticsVos.add(agentDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(agentDataTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(agentDataTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    BigDecimal amt = map.get(date);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }

                    AgentDataTrendStatisticsVo agentDataTrendStatisticsVo = new AgentDataTrendStatisticsVo();
                    agentDataTrendStatisticsVo.setDate(date);
                    agentDataTrendStatisticsVo.setValue(amt.toString());
                    agentDataTrendStatisticsVos.add(agentDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return agentDataTrendStatisticsVos;
        } else if (type == 2) {
            // 分销订单数
            Map<String, Integer> map = new HashMap<>();

            OrderDto orderDto = new OrderDto();
            orderDto.setBusinessId(businessId);
            orderDto.setNoAgentId(0L);
            orderDto.setStartOrderTime(agentDataTrendStatisticsDto.getStartTime());
            orderDto.setEndOrderTime(agentDataTrendStatisticsDto.getEndTime());
            orderDto.setOrderStates(Arrays.asList(OrderStateEnum.FINISH.getValue(),
                    OrderStateEnum.WAIT_VERIFY.getValue(),
                    OrderStateEnum.VERIFYING.getValue(),
                    OrderStateEnum.VERIFIED.getValue()));
            List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
            if (!CollectionUtils.isEmpty(orderVos)) {
                for (OrderVo orderVo : orderVos) {
                    // 下单时间
                    LocalDateTime orderTime = orderVo.getOrderTime();
                    String mapKey = getMapKey(dateType, orderTime);
                    Integer count = map.get(mapKey);
                    if (count == null) {
                        count = 0;
                    }
                    count += 1;
                    map.put(mapKey, count);
                }
            }


//            OrderItemDto orderItemDto = new OrderItemDto();
//            orderItemDto.setBusinessId(businessId);
//            orderItemDto.setNoAgentId(0L);
//            orderItemDto.setStartOrderTime(agentDataTrendStatisticsDto.getStartTime());
//            orderItemDto.setEndOrderTime(agentDataTrendStatisticsDto.getEndTime());
//            List<OrderItemVo> orderItemVos = orderItemApi.listByDto(orderItemDto).unpack();
//            if (!CollectionUtils.isEmpty(orderItemVos)) {
//                // 成功下单的订单项列表
//                List<OrderItemVo> orderItemVoList = orderItemVos.stream().filter(orderItemVo ->
//                        (SubOrderStateEnum.WAIT_VERIFY.getValue().equals(orderItemVo.getSubOrderState())
//                                || SubOrderStateEnum.VERIFIED.getValue().equals(orderItemVo.getSubOrderState()))).collect(Collectors.toList());
//                if (!CollectionUtils.isEmpty(orderItemVoList)) {
//                    for (OrderItemVo orderItemVo : orderItemVoList) {
//                        // 下单时间
//                        LocalDateTime orderTime = orderItemVo.getOrderTime();
//                        String mapKey = getMapKey(dateType, orderTime);
////                        String mapKey = LocalDateUtil.toLocalDateToString(orderTime.toLocalDate(), key);
//                        List<OrderItemVo> orderItems = map.get(mapKey);
//                        if (CollectionUtils.isEmpty(orderItems)) {
//                            orderItems = new ArrayList<>();
//                        }
//                        orderItems.add(orderItemVo);
//                        map.put(mapKey, orderItems);
//                    }
//                }
//            }


            List<AgentDataTrendStatisticsVo> agentDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    int orderNum = 0;
                    Integer count = map.get(date);
                    if (count != null) {
                        orderNum = count;
                    }

                    AgentDataTrendStatisticsVo agentDataTrendStatisticsVo = new AgentDataTrendStatisticsVo();
                    agentDataTrendStatisticsVo.setDate(date);
                    agentDataTrendStatisticsVo.setValue(String.valueOf(orderNum));
                    agentDataTrendStatisticsVos.add(agentDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(agentDataTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(agentDataTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    int orderNum = 0;
                    Integer count = map.get(date);
                    if (count != null) {
                        orderNum = count;
                    }

                    AgentDataTrendStatisticsVo agentDataTrendStatisticsVo = new AgentDataTrendStatisticsVo();
                    agentDataTrendStatisticsVo.setDate(date);
                    agentDataTrendStatisticsVo.setValue(String.valueOf(orderNum));
                    agentDataTrendStatisticsVos.add(agentDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return agentDataTrendStatisticsVos;
        } else if (type == 3) {
            // 分销退款金额
            Map<String, BigDecimal> map = new HashMap<>();

            // 根据日期时间查询商家所有的订单数据,获取分销订单金额,分销订单数,分销退款金额,分销退款订单数,已结算佣金
            OrderDto orderDto = new OrderDto();
            orderDto.setBusinessId(businessId);
            orderDto.setNoAgentId(0L);
            orderDto.setStartOrderTime(agentDataTrendStatisticsDto.getStartTime());
            orderDto.setEndOrderTime(agentDataTrendStatisticsDto.getEndTime());
            orderDto.setOrderStates(Collections.singletonList(OrderStateEnum.CANCEL.getValue()));
            List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
            if (!CollectionUtils.isEmpty(orderVos)) {
                for (OrderVo orderVo : orderVos) {
                    // 下单时间
                    LocalDateTime orderTime = orderVo.getOrderTime();
                    String mapKey = getMapKey(dateType, orderTime);
                    BigDecimal amt = map.get(mapKey);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }
                    amt = amt.add(orderVo.getPayPrice());
                    map.put(mapKey, amt);
                }
            }


//            OrderItemDto orderItemDto = new OrderItemDto();
//            if (businessId != null) {
//                orderItemDto.setBusinessId(businessId);
//                orderItemDto.setNoAgentId(0L);
//            } else {
//                orderItemDto.setAgentId(userId);
//            }
//            orderItemDto.setStartOrderTime(agentDataTrendStatisticsDto.getStartTime());
//            orderItemDto.setEndOrderTime(agentDataTrendStatisticsDto.getEndTime());
//            List<OrderItemVo> orderItemVos = orderItemApi.listByDto(orderItemDto).unpack();
//            if (!CollectionUtils.isEmpty(orderItemVos)) {
//                // 取消下单的订单项列表
//                List<OrderItemVo> orderItemVoList = orderItemVos.stream().filter(orderItemVo ->
//                        (SubOrderStateEnum.CANCEL.getValue().equals(orderItemVo.getSubOrderState())
//                                || SubOrderStateEnum.EXPIRED.getValue().equals(orderItemVo.getSubOrderState()))).collect(Collectors.toList());
//                if (!CollectionUtils.isEmpty(orderItemVoList)) {
//                    for (OrderItemVo orderItemVo : orderItemVoList) {
//                        // 商户订单本金(按比例分配的用户支付的订单款)
//                        BigDecimal merchantAmt = orderItemVo.getMerchantAmt();
//                        BigDecimal distributionOrderAmt = orderItemVo.getDistributionOrderAmt();
//                        // 下单时间
//                        LocalDateTime orderTime = orderItemVo.getOrderTime();
//                        String mapKey = getMapKey(dateType, orderTime);
////                        String mapKey = LocalDateUtil.toLocalDateToString(orderTime.toLocalDate(), key);
//                        BigDecimal amt = map.get(mapKey);
//                        if (amt == null) {
//                            amt = BigDecimal.ZERO;
//                        }
//                        amt = amt.add(distributionOrderAmt);
//                        map.put(mapKey, amt);
//                    }
//                }
//            }

            List<AgentDataTrendStatisticsVo> agentDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    BigDecimal amt = map.get(date);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }

                    AgentDataTrendStatisticsVo agentDataTrendStatisticsVo = new AgentDataTrendStatisticsVo();
                    agentDataTrendStatisticsVo.setDate(date);
                    agentDataTrendStatisticsVo.setValue(amt.toString());
                    agentDataTrendStatisticsVos.add(agentDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(agentDataTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(agentDataTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    BigDecimal amt = map.get(date);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }

                    AgentDataTrendStatisticsVo agentDataTrendStatisticsVo = new AgentDataTrendStatisticsVo();
                    agentDataTrendStatisticsVo.setDate(date);
                    agentDataTrendStatisticsVo.setValue(amt.toString());
                    agentDataTrendStatisticsVos.add(agentDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return agentDataTrendStatisticsVos;
        } else if (type == 4) {
            // 分销退款订单数
            Map<String, Integer> map = new HashMap<>();

            // 根据日期时间查询商家所有的订单数据,获取分销订单金额,分销订单数,分销退款金额,分销退款订单数,已结算佣金
            OrderDto orderDto = new OrderDto();
            orderDto.setBusinessId(businessId);
            orderDto.setNoAgentId(0L);
            orderDto.setStartOrderTime(agentDataTrendStatisticsDto.getStartTime());
            orderDto.setEndOrderTime(agentDataTrendStatisticsDto.getEndTime());
            orderDto.setOrderStates(Collections.singletonList(OrderStateEnum.CANCEL.getValue()));
            List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
            if (!CollectionUtils.isEmpty(orderVos)) {
                for (OrderVo orderVo : orderVos) {
                    // 下单时间
                    LocalDateTime orderTime = orderVo.getOrderTime();
                    String mapKey = getMapKey(dateType, orderTime);
                    Integer count = map.get(mapKey);
                    if (count == null) {
                        count = 0;
                    }
                    count += 1;
                    map.put(mapKey, count);
                }
            }


//            OrderItemDto orderItemDto = new OrderItemDto();
//            if (businessId != null) {
//                orderItemDto.setBusinessId(businessId);
//                orderItemDto.setNoAgentId(0L);
//            } else {
//                orderItemDto.setAgentId(userId);
//            }
//            orderItemDto.setStartOrderTime(agentDataTrendStatisticsDto.getStartTime());
//            orderItemDto.setEndOrderTime(agentDataTrendStatisticsDto.getEndTime());
//            List<OrderItemVo> orderItemVos = orderItemApi.listByDto(orderItemDto).unpack();
//            if (!CollectionUtils.isEmpty(orderItemVos)) {
//                // 取消下单的订单项列表
//                List<OrderItemVo> orderItemVoList = orderItemVos.stream().filter(orderItemVo ->
//                        (SubOrderStateEnum.CANCEL.getValue().equals(orderItemVo.getSubOrderState())
//                                || SubOrderStateEnum.EXPIRED.getValue().equals(orderItemVo.getSubOrderState()))).collect(Collectors.toList());
//                if (!CollectionUtils.isEmpty(orderItemVoList)) {
//                    for (OrderItemVo orderItemVo : orderItemVoList) {
//                        // 下单时间
//                        LocalDateTime orderTime = orderItemVo.getOrderTime();
//                        String mapKey = getMapKey(dateType, orderTime);
////                        String mapKey = LocalDateUtil.toLocalDateToString(orderTime.toLocalDate(), key);
//                        List<OrderItemVo> orderItems = map.get(mapKey);
//                        if (CollectionUtils.isEmpty(orderItems)) {
//                            orderItems = new ArrayList<>();
//                        }
//                        orderItems.add(orderItemVo);
//                        map.put(mapKey, orderItems);
//                    }
//                }
//            }


            List<AgentDataTrendStatisticsVo> agentDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    int orderNum = 0;
                    Integer count = map.get(date);
                    if (count != null) {
                        orderNum = count;
                    }

                    AgentDataTrendStatisticsVo agentDataTrendStatisticsVo = new AgentDataTrendStatisticsVo();
                    agentDataTrendStatisticsVo.setDate(date);
                    agentDataTrendStatisticsVo.setValue(String.valueOf(orderNum));
                    agentDataTrendStatisticsVos.add(agentDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(agentDataTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(agentDataTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    int orderNum = 0;
                    Integer count = map.get(date);
                    if (count != null) {
                        orderNum = count;
                    }

                    AgentDataTrendStatisticsVo agentDataTrendStatisticsVo = new AgentDataTrendStatisticsVo();
                    agentDataTrendStatisticsVo.setDate(date);
                    agentDataTrendStatisticsVo.setValue(String.valueOf(orderNum));
                    agentDataTrendStatisticsVos.add(agentDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return agentDataTrendStatisticsVos;
        } else if (type == 5) {
            // 新增经纪人数
            Map<String, List<BusinessAgentVo>> map = new HashMap<>();

            if (businessId != null && businessId > 0) {
                BusinessAgentDto businessAgentDto = new BusinessAgentDto();
                businessAgentDto.setBusinessId(businessId);
                businessAgentDto.setStartTime(agentDataTrendStatisticsDto.getStartTime());
                businessAgentDto.setEndTime(agentDataTrendStatisticsDto.getEndTime());
                List<BusinessAgentVo> businessAgentVos = businessAgentApi.listByDto(businessAgentDto).unpack();
                if (!CollectionUtils.isEmpty(businessAgentVos)) {
                    for (BusinessAgentVo businessAgentVo : businessAgentVos) {
                        LocalDateTime createTime = businessAgentVo.getCreateTime();
                        String mapKey = getMapKey(dateType, createTime);
                        List<BusinessAgentVo> businessAgentVoList = map.get(mapKey);
                        if (CollectionUtils.isEmpty(businessAgentVoList)) {
                            businessAgentVoList = new ArrayList<>();
                        }
                        businessAgentVoList.add(businessAgentVo);
                        map.put(mapKey, businessAgentVoList);
                    }
                }
            }


            List<AgentDataTrendStatisticsVo> agentDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    int businessAgentUserNum = 0;
                    List<BusinessAgentVo> businessAgentVoList = map.get(date);
                    if (!CollectionUtils.isEmpty(businessAgentVoList)) {
                        businessAgentUserNum = businessAgentVoList.stream().map(BusinessAgentVo::getUserId).collect(Collectors.toSet()).size();
                    }

                    AgentDataTrendStatisticsVo agentDataTrendStatisticsVo = new AgentDataTrendStatisticsVo();
                    agentDataTrendStatisticsVo.setDate(date);
                    agentDataTrendStatisticsVo.setValue(String.valueOf(businessAgentUserNum));
                    agentDataTrendStatisticsVos.add(agentDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(agentDataTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(agentDataTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    int businessAgentUserNum = 0;
                    List<BusinessAgentVo> businessAgentVoList = map.get(date);
                    if (!CollectionUtils.isEmpty(businessAgentVoList)) {
                        businessAgentUserNum = businessAgentVoList.stream().map(BusinessAgentVo::getUserId).collect(Collectors.toSet()).size();
                    }

                    AgentDataTrendStatisticsVo agentDataTrendStatisticsVo = new AgentDataTrendStatisticsVo();
                    agentDataTrendStatisticsVo.setDate(date);
                    agentDataTrendStatisticsVo.setValue(String.valueOf(businessAgentUserNum));
                    agentDataTrendStatisticsVos.add(agentDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return agentDataTrendStatisticsVos;
        } else if (type == 6) {
            // 已结算佣金
            Map<String, BigDecimal> map = new HashMap<>();

            OrderDto orderDto = new OrderDto();
            orderDto.setBusinessId(businessId);
            orderDto.setStartOrderTime(agentDataTrendStatisticsDto.getStartTime());
            orderDto.setEndOrderTime(agentDataTrendStatisticsDto.getEndTime());
            orderDto.setOrderStates(Collections.singletonList(OrderStateEnum.FINISH.getValue()));
            List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
            if (!CollectionUtils.isEmpty(orderVos)) {
                List<Long> orderIds = orderVos.stream().map(OrderVo::getId).collect(Collectors.toList());

                Map<Long, List<OrderItemVo>> orderItemVoMap = new HashMap<>();
                OrderItemDto orderItemDto = new OrderItemDto();
                orderItemDto.setBusinessId(businessId);
                orderItemDto.setOrderIds(orderIds);
                orderItemDto.setSubOrderStates(Collections.singletonList(SubOrderStateEnum.VERIFIED.getValue()));
                List<OrderItemVo> orderItemVos = orderItemApi.listByDto(orderItemDto).unpack();
                if (!CollectionUtils.isEmpty(orderItemVos)) {
                    orderItemVoMap = orderItemVos.stream().collect(Collectors.groupingBy(OrderItemVo::getOrderId));
                }

                for (OrderVo orderVo : orderVos) {
                    List<OrderItemVo> orderItemVoList = orderItemVoMap.get(orderVo.getId());
                    if (!CollectionUtils.isEmpty(orderItemVoList)) {
                        for (OrderItemVo orderItemVo : orderItemVoList) {
                            // 佣金
                            BigDecimal commissionAmt = orderItemVo.getCommissionAmt();
                            // 下单时间
                            LocalDateTime orderTime = orderItemVo.getOrderTime();
                            String mapKey = getMapKey(dateType, orderTime);
                            BigDecimal amt = map.get(mapKey);
                            if (amt == null) {
                                amt = BigDecimal.ZERO;
                            }
                            amt = amt.add(commissionAmt);
                            map.put(mapKey, amt);
                        }
                    }
                }
            }


//            if (businessId != null && businessId > 0) {
//                OrderItemDto orderItemDto = new OrderItemDto();
//                orderItemDto.setBusinessId(businessId);
//                orderItemDto.setStartOrderTime(agentDataTrendStatisticsDto.getStartTime());
//                orderItemDto.setEndOrderTime(agentDataTrendStatisticsDto.getEndTime());
//                List<OrderItemVo> orderItemVos = orderItemApi.listByDto(orderItemDto).unpack();
//                if (!CollectionUtils.isEmpty(orderItemVos)) {
//                    // 已结算的订单项列表
//                    List<OrderItemVo> orderItemVoList = orderItemVos.stream().filter(orderItemVo ->
//                            SettleStateEnum.YES.getValue().equals(orderItemVo.getSubOrderState())).collect(Collectors.toList());
//
//                    // 成功下单的订单项列表
////                    List<OrderItemVo> orderItemVoList = orderItemVos.stream().filter(orderItemVo ->
////                            (SubOrderStateEnum.WAIT_VERIFY.getValue().equals(orderItemVo.getSubOrderState())
////                                    || SubOrderStateEnum.VERIFIED.getValue().equals(orderItemVo.getSubOrderState()))).collect(Collectors.toList());
//                    if (!CollectionUtils.isEmpty(orderItemVoList)) {
//                        for (OrderItemVo orderItemVo : orderItemVoList) {
//                            // 佣金
//                            BigDecimal commissionAmt = orderItemVo.getCommissionAmt();
//                            // 下单时间
//                            LocalDateTime orderTime = orderItemVo.getOrderTime();
//                            String mapKey = getMapKey(dateType, orderTime);
////                            String mapKey = LocalDateUtil.toLocalDateToString(orderTime.toLocalDate(), key);
//                            BigDecimal amt = map.get(mapKey);
//                            if (amt == null) {
//                                amt = BigDecimal.ZERO;
//                            }
//                            amt = amt.add(commissionAmt);
//                            map.put(mapKey, amt);
//                        }
//                    }
//                }
//            }


            List<AgentDataTrendStatisticsVo> agentDataTrendStatisticsVos = new ArrayList<>();
            if (dateType == 1) {
                for (int i = 0; i < 24; i++) {
                    String date = i < 10 ? ("0" + i) : String.valueOf(i);
                    BigDecimal amt = map.get(date);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }

                    AgentDataTrendStatisticsVo agentDataTrendStatisticsVo = new AgentDataTrendStatisticsVo();
                    agentDataTrendStatisticsVo.setDate(date);
                    agentDataTrendStatisticsVo.setValue(amt.toString());
                    agentDataTrendStatisticsVos.add(agentDataTrendStatisticsVo);
                }
            } else {
                LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(agentDataTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(agentDataTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
                long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
                for (int i = 0; i < days; i++) {
                    String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);
                    BigDecimal amt = map.get(date);
                    if (amt == null) {
                        amt = BigDecimal.ZERO;
                    }

                    AgentDataTrendStatisticsVo agentDataTrendStatisticsVo = new AgentDataTrendStatisticsVo();
                    agentDataTrendStatisticsVo.setDate(date);
                    agentDataTrendStatisticsVo.setValue(amt.toString());
                    agentDataTrendStatisticsVos.add(agentDataTrendStatisticsVo);

                    startLocalDate = startLocalDate.plusDays(1);
                }
            }

            return agentDataTrendStatisticsVos;
        }
        return null;
    }
}
